Skip to content

Commit

Permalink
Harmonize functions to convert from Polars to R (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Mar 19, 2024
1 parent bf02ca7 commit 2b0dc7c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 99 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
- In `pl$col()`, the `name` argument is removed, and the `...` argument no longer
accepts a list of characters and `RPolarsSeries` class objects (#923).
- Removed `$argsort()` which was an old alias for `$arg_sort()` (#930).
- Removed `pl$expr_to_r()` which was an alias for `$to_r()` (#938).
- `<Series>$to_r_list()` is renamed `<Series>$to_list()` (#938).
- Removed `<Series>$to_r_vector()` which was an old alias for
`<Series>$to_vector()` (#938).

### New features

Expand Down
17 changes: 0 additions & 17 deletions R/expr__expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -3077,23 +3077,6 @@ Expr_to_r = function(df = NULL, i = 0, ..., int64_conversion = polars_options()$
}
}

#' Convert an Expr to R output
#'
#' This is mostly useful to debug an expression. It evaluates the Expr in an
#' empty DataFrame and return the first Series to R. This is an alias for
#' `$to_r()`.
#' @param expr An Expr to evaluate.
#' @param df If `NULL` (default), it evaluates the Expr in an empty DataFrame.
#' Otherwise, provide a DataFrame that the Expr should be evaluated in.
#' @param i Numeric column to extract. Default is zero (which gives the first
#' column).
#' @return R object
#' @examples
#' pl$expr_to_r(pl$lit(1:3))
pl_expr_to_r = function(expr, df = NULL, i = 0) {
wrap_e(expr)$to_r(df, i)
}

#' Value counts
#' @description
#' Count all unique values and create a struct mapping value to count.
Expand Down
15 changes: 5 additions & 10 deletions R/series__series.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ Series_compare = function(other, op) {
#'
#' `$to_r()` automatically returns an R vector or list based on the Polars
#' DataType. It is possible to force the output type by using `$to_vector()` or
#' `$to_r_list()`.
#' `$to_list()`.
#'
#' @inheritParams DataFrame_to_data_frame
#'
Expand All @@ -487,7 +487,7 @@ Series_compare = function(other, op) {
#' series_vec = pl$Series(letters[1:3])
#'
#' series_vec$to_r() # as vector because Series DataType is not list (is String)
#' series_vec$to_r_list() # implicit call as.list(), convert to list
#' series_vec$to_list() # implicit call as.list(), convert to list
#' series_vec$to_vector() # implicit call unlist(), same as to_r() as already vector
#'
#'
Expand All @@ -501,7 +501,7 @@ Series_compare = function(other, op) {
#' series_list
#'
#' series_list$to_r() # as list because Series DataType is list
#' series_list$to_r_list() # implicit call as.list(), same as to_r() as already list
#' series_list$to_list() # implicit call as.list(), same as to_r() as already list
#' series_list$to_vector() # implicit call unlist(), append into a vector
Series_to_r = \(int64_conversion = polars_options()$int64_conversion) {
unwrap(.pr$Series$to_r(self, int64_conversion), "in $to_r():")
Expand All @@ -513,15 +513,10 @@ Series_to_vector = \(int64_conversion = polars_options()$int64_conversion) {
unlist(unwrap(.pr$Series$to_r(self, int64_conversion)), "in $to_vector():")
}

#' Alias to Series_to_vector (backward compatibility)
#' @return R vector
#' @noRd
Series_to_r_vector = Series_to_vector

#' @rdname Series_to_r
#' @inheritParams DataFrame_to_data_frame
Series_to_r_list = \(int64_conversion = polars_options()$int64_conversion) {
as.list(unwrap(.pr$Series$to_r(self, int64_conversion)), "in $to_r_list():")
Series_to_list = \(int64_conversion = polars_options()$int64_conversion) {
as.list(unwrap(.pr$Series$to_r(self, int64_conversion)), "in $to_list():")
}

#' Count the occurrences of unique values
Expand Down
10 changes: 5 additions & 5 deletions man/Series_to_r.Rd

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

28 changes: 0 additions & 28 deletions man/pl_expr_to_r.Rd

This file was deleted.

2 changes: 1 addition & 1 deletion man/pl_pl.Rd

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

59 changes: 29 additions & 30 deletions tests/testthat/_snaps/after-wrappers.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,30 @@
[45] "date_range" "datetime"
[47] "disable_string_cache" "dtypes"
[49] "duration" "element"
[51] "enable_string_cache" "expr_to_r"
[53] "first" "fold"
[55] "from_epoch" "get_global_rpool_cap"
[57] "head" "implode"
[59] "is_schema" "last"
[61] "len" "lit"
[63] "max" "max_horizontal"
[65] "mean" "median"
[67] "mem_address" "min"
[69] "min_horizontal" "n_unique"
[71] "numeric_dtypes" "raw_list"
[73] "read_csv" "read_ndjson"
[75] "read_parquet" "reduce"
[77] "rolling_corr" "rolling_cov"
[79] "same_outer_dt" "scan_csv"
[81] "scan_ipc" "scan_ndjson"
[83] "scan_parquet" "select"
[85] "set_global_rpool_cap" "show_all_public_functions"
[87] "show_all_public_methods" "std"
[89] "struct" "sum"
[91] "sum_horizontal" "tail"
[93] "thread_pool_size" "threadpool_size"
[95] "time" "using_string_cache"
[97] "var" "when"
[99] "with_string_cache"
[51] "enable_string_cache" "first"
[53] "fold" "from_epoch"
[55] "get_global_rpool_cap" "head"
[57] "implode" "is_schema"
[59] "last" "len"
[61] "lit" "max"
[63] "max_horizontal" "mean"
[65] "median" "mem_address"
[67] "min" "min_horizontal"
[69] "n_unique" "numeric_dtypes"
[71] "raw_list" "read_csv"
[73] "read_ndjson" "read_parquet"
[75] "reduce" "rolling_corr"
[77] "rolling_cov" "same_outer_dt"
[79] "scan_csv" "scan_ipc"
[81] "scan_ndjson" "scan_parquet"
[83] "select" "set_global_rpool_cap"
[85] "show_all_public_functions" "show_all_public_methods"
[87] "std" "struct"
[89] "sum" "sum_horizontal"
[91] "tail" "thread_pool_size"
[93] "threadpool_size" "time"
[95] "using_string_cache" "var"
[97] "when" "with_string_cache"

---

Expand Down Expand Up @@ -680,11 +679,11 @@
[166] "std" "str" "struct"
[169] "sub" "sum" "tail"
[172] "tan" "tanh" "to_frame"
[175] "to_lit" "to_physical" "to_r"
[178] "to_r_list" "to_r_vector" "to_struct"
[181] "to_vector" "top_k" "unique"
[184] "unique_counts" "upper_bound" "value_counts"
[187] "var" "xor"
[175] "to_list" "to_lit" "to_physical"
[178] "to_r" "to_struct" "to_vector"
[181] "top_k" "unique" "unique_counts"
[184] "upper_bound" "value_counts" "var"
[187] "xor"

---

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-expr_expr.R
Original file line number Diff line number Diff line change
Expand Up @@ -2167,7 +2167,6 @@ test_that("to_r", {
NA, NA_integer_, NA_character_, NA_real_
)
for (i in l) expect_identical(pl$lit(i)$to_r(), i)
for (i in l) expect_identical(pl$expr_to_r(i), i)

# NULL to NULL
expect_identical(pl$lit(NULL)$to_r(), NULL)
Expand Down
9 changes: 2 additions & 7 deletions tests/testthat/test-series.R
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ test_that("Series list", {

expected_list = list(list(c(1L, 2L, 3L, 4L, 5L, NA)), list(1:2, NA_integer_))
expect_identical(series_list$to_r(), expected_list)
expect_identical(series_list$to_r_list(), expected_list)
expect_identical(series_list$to_list(), expected_list)
expect_identical(series_list$to_vector(), unlist(expected_list))

series_vec = pl$Series(1:5)
expect_identical(series_vec$to_r(), 1:5)
expect_identical(series_vec$to_vector(), 1:5)
expect_identical(series_vec$to_r_list(), as.list(1:5))
expect_identical(series_vec$to_list(), as.list(1:5))


# build heterogenous sized 3-level nested list of chars
Expand Down Expand Up @@ -515,11 +515,6 @@ test_that("to_series", {
expect_identical(pl$DataFrame(l)$to_series(2), NULL)
})


test_that("Backward compatibility: to_r_vector", {
expect_identical(pl$Series(1:3)$to_r_vector(), 1:3)
})

test_that("internal method get_fmt and to_fmt_char", {
s_1 = pl$Series(c("foo", "bar"))
expect_equal(
Expand Down

0 comments on commit 2b0dc7c

Please sign in to comment.