Skip to content

Commit

Permalink
Add remaining subnamespaces for Series (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Feb 21, 2024
1 parent ff1bd11 commit f3a71df
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 62 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
- New active bindings `$flags` for `DataFrame` to show the flags used internally
for each column. The output of `$flags` for `Series` was also improved and now
contains `FAST_EXPLODE` for `Series` of type `list` and `array` (#809).
- `$str` sub namespace is added to `Series` (#819).
- All subnamespaces that can be used on `Expr` are now available for `Series`
(#819, #828).
- `as_polars_df()` for `data.frame` is more memory-efficient and new arguments
`schema` and `schema_overrides` are added (#817).
- Use `polars_code_completion_activate()` to enable code suggestions and
Expand Down
8 changes: 5 additions & 3 deletions R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#'
#' `$cat` stores all categorical related methods.
#'
#' ## dt
#'
#' `$dt` stores all temporal related methods.
#'
#' ## list
#'
#' `$list` stores all list related methods.
Expand All @@ -38,11 +42,9 @@
#'
#' `$struct` stores all struct related methods.
#'
#' ## dt
#'
#' `$dt` stores all temporal related methods.
#' @name Expr_class
#' @aliases RPolarsExpr
#'
#' @examples
#' df = pl$DataFrame(
#' a = 1:2,
Expand Down
61 changes: 43 additions & 18 deletions R/series__series.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,24 @@
#' `$shape` returns a numeric vector of length two with the number of length
#' of the Series and width of the Series (always 1).
#'
#'
#' @section Sub-namespaces:
#'
#' Some functions are stored under active bindings, so it works like a sub-namespaces.
#' ## arr
#'
#' `$arr` stores all array related methods.
#'
#' ## bin
#'
#' `$bin` stores all binary related methods.
#'
#' ## cat
#'
#' `$cat` stores all categorical related methods.
#'
#' ## dt
#'
#' `$dt` stores all temporal related methods.
#'
#' ## expr
#'
Expand All @@ -67,21 +82,20 @@
#'
#' ## list
#'
#' `$list` calls functions in `<Expr>$list`.
#' `$list` stores all list related methods.
#'
#' ## str
#'
#' `$str` calls functions in `<Expr>$str`.
#' `$str` stores all string related methods.
#'
#' @keywords Series
#' ## struct
#'
#' @examples
#' pl$show_all_public_methods("RPolarsSeries")
#' `$struct` stores all struct related methods.
#'
#' # see all private methods (not intended for regular use)
#' ls(.pr$Series)
#' @keywords Series
#'
#' # make an object
#' @examples
#' # make a Series
#' s = pl$Series(c(1:3, 1L))
#'
#' # call an active binding
Expand All @@ -90,17 +104,20 @@
#' # show flags
#' s$sort()$flags
#'
#' # use a private method (mutable append not allowed in public api)
#' s_copy = s
#' .pr$Series$append_mut(s, pl$Series(5:1))
#' identical(s_copy$to_r(), s$to_r()) # s_copy was modified when s was modified
#' # use subnamespaces
#' pl$Series(list(3:1, 1:2, NULL))$list$first()
#' pl$Series(c(1, NA, 2))$str$concat("-")
#'
#' # call functions via sub-namespaces
#' pl$Series(c(1:3))$expr$add(1)
#' s = pl$date_range(
#' as.Date("2024-02-18"), as.Date("2024-02-24"),
#' interval = "1d"
#' )$to_series()
#' s
#' s$dt$day()
#'
#' pl$Series(list(3:1, 1:2, NULL))$list$first()
#' # show all available methods for Series
#' pl$show_all_public_methods("RPolarsSeries")
#'
#' pl$Series(c(1, NA, 2))$str$concat("-")
NULL


Expand Down Expand Up @@ -180,12 +197,20 @@ series_make_sub_ns = function(pl_series, .expr_make_sub_ns_fn) {
})
}

Series_arr = method_as_active_binding(\() series_make_sub_ns(self, expr_arr_make_sub_ns))

Series_list = method_as_active_binding(\() series_make_sub_ns(self, expr_list_make_sub_ns))
Series_bin = method_as_active_binding(\() series_make_sub_ns(self, expr_bin_make_sub_ns))

Series_cat = method_as_active_binding(\() series_make_sub_ns(self, expr_cat_make_sub_ns))

Series_dt = method_as_active_binding(\() series_make_sub_ns(self, expr_dt_make_sub_ns))

Series_list = method_as_active_binding(\() series_make_sub_ns(self, expr_list_make_sub_ns))

Series_str = method_as_active_binding(\() series_make_sub_ns(self, expr_str_make_sub_ns))

Series_struct = method_as_active_binding(\() series_make_sub_ns(self, expr_struct_make_sub_ns))


#' Wrap as Series
#' @noRd
Expand Down
10 changes: 5 additions & 5 deletions man/Expr_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 38 additions & 17 deletions man/Series_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions tests/testthat/_snaps/after-wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,17 +498,18 @@
Output
[1] "abs" "add" "alias" "all"
[5] "any" "append" "arg_max" "arg_min"
[9] "ceil" "chunk_lengths" "clone" "compare"
[13] "cum_sum" "div" "dtype" "equals"
[17] "expr" "flags" "floor" "is_numeric"
[21] "is_sorted" "len" "list" "map_elements"
[25] "max" "mean" "median" "min"
[29] "mul" "n_unique" "name" "print"
[33] "rem" "rename" "rep" "set_sorted"
[37] "shape" "sort" "std" "str"
[41] "sub" "sum" "to_frame" "to_lit"
[45] "to_r" "to_r_list" "to_r_vector" "to_vector"
[49] "value_counts" "var"
[9] "arr" "bin" "cat" "ceil"
[13] "chunk_lengths" "clone" "compare" "cum_sum"
[17] "div" "dt" "dtype" "equals"
[21] "expr" "flags" "floor" "is_numeric"
[25] "is_sorted" "len" "list" "map_elements"
[29] "max" "mean" "median" "min"
[33] "mul" "n_unique" "name" "print"
[37] "rem" "rename" "rep" "set_sorted"
[41] "shape" "sort" "std" "str"
[45] "struct" "sub" "sum" "to_frame"
[49] "to_lit" "to_r" "to_r_list" "to_r_vector"
[53] "to_vector" "value_counts" "var"

---

Expand Down
46 changes: 39 additions & 7 deletions tests/testthat/test-series-sub-namespace.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
test_that("Series to lower case", {
test_that("string sub namespace", {
expect_identical(
pl$Series(c("A", "B", NA, "D"))$str$to_lowercase()$to_r(),
c("a", "b", NA, "d")
)
})

test_that("Series to upper case", {
test_that("List sub namespace", {
expect_identical(
pl$Series(c("a", "b", NA, "d"))$str$to_uppercase()$to_r(),
c("A", "B", NA, "D")
pl$Series(list(3:1, 1:2, NULL))$list$first()$to_r(),
c(3L, 1L, NA)
)
})

test_that("List sub namespace", {
test_that("datetime sub namespace", {
s = pl$date_range(
as.Date("2024-02-18"), as.Date("2024-02-24"),
interval = "1d"
)$to_series()

expect_identical(
pl$Series(list(3:1, 1:2, NULL))$list$first()$to_r(),
c(3L, 1L, NA)
s$dt$day()$to_r(),
18:24
)
})

test_that("binary subnamespace", {
s = pl$Series(c(r"(\x00\x00\x00)", r"(\xff\xff\x00)", r"(\x00\x00\xff)"))$
to_lit()$
cast(pl$Binary)$
to_series()
expect_identical(
s$bin$contains(pl$lit(r"(\xff)")$cast(pl$Binary))$to_r(),
c(FALSE, TRUE, TRUE)
)
})

test_that("categorical sub namespace", {
s = pl$Series(factor(c("foo", "bar", "foo", "foo", "ham")))
expect_identical(
s$cat$get_categories()$to_r(),
c("foo", "bar", "ham")
)
})

# TODO: this panicks
# test_that("array sub namespace", {
# s = pl$Series(list(3:1, 1:2, c(NA_integer_, 4L)))$
# to_lit()$
# cast(pl$Array(width = 2))$
# to_series()
# })

0 comments on commit f3a71df

Please sign in to comment.