From 2b0dc7cbf9d4b8b6a9327d9c62866978ef995508 Mon Sep 17 00:00:00 2001 From: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:12:14 +0100 Subject: [PATCH] Harmonize functions to convert from Polars to R (#938) --- NEWS.md | 4 ++ R/expr__expr.R | 17 ------- R/series__series.R | 15 +++---- man/Series_to_r.Rd | 10 ++--- man/pl_expr_to_r.Rd | 28 ------------ man/pl_pl.Rd | 2 +- tests/testthat/_snaps/after-wrappers.md | 59 ++++++++++++------------- tests/testthat/test-expr_expr.R | 1 - tests/testthat/test-series.R | 9 +--- 9 files changed, 46 insertions(+), 99 deletions(-) delete mode 100644 man/pl_expr_to_r.Rd diff --git a/NEWS.md b/NEWS.md index b94195288..7e9b78b2e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). +- `$to_r_list()` is renamed `$to_list()` (#938). +- Removed `$to_r_vector()` which was an old alias for + `$to_vector()` (#938). ### New features diff --git a/R/expr__expr.R b/R/expr__expr.R index 38f1fadb1..732d395b6 100644 --- a/R/expr__expr.R +++ b/R/expr__expr.R @@ -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. diff --git a/R/series__series.R b/R/series__series.R index 1cc1b9dff..df9083f5b 100644 --- a/R/series__series.R +++ b/R/series__series.R @@ -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 #' @@ -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 #' #' @@ -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():") @@ -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 diff --git a/man/Series_to_r.Rd b/man/Series_to_r.Rd index f7953b29a..216fb04bf 100644 --- a/man/Series_to_r.Rd +++ b/man/Series_to_r.Rd @@ -3,14 +3,14 @@ \name{Series_to_r} \alias{Series_to_r} \alias{Series_to_vector} -\alias{Series_to_r_list} +\alias{Series_to_list} \title{Convert Series to R vector or list} \usage{ Series_to_r(int64_conversion = polars_options()$int64_conversion) Series_to_vector(int64_conversion = polars_options()$int64_conversion) -Series_to_r_list(int64_conversion = polars_options()$int64_conversion) +Series_to_list(int64_conversion = polars_options()$int64_conversion) } \arguments{ \item{int64_conversion}{How should Int64 values be handled when converting a @@ -28,7 +28,7 @@ R list or vector \description{ \verb{$to_r()} automatically returns an R vector or list based on the Polars DataType. It is possible to force the output type by using \verb{$to_vector()} or -\verb{$to_r_list()}. +\verb{$to_list()}. } \section{Conversion to R data types considerations}{ @@ -79,7 +79,7 @@ withr::with_envvar( 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 @@ -93,6 +93,6 @@ series_list = pl$Series( 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 } diff --git a/man/pl_expr_to_r.Rd b/man/pl_expr_to_r.Rd deleted file mode 100644 index 461c08048..000000000 --- a/man/pl_expr_to_r.Rd +++ /dev/null @@ -1,28 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/expr__expr.R -\name{pl_expr_to_r} -\alias{pl_expr_to_r} -\title{Convert an Expr to R output} -\usage{ -pl_expr_to_r(expr, df = NULL, i = 0) -} -\arguments{ -\item{expr}{An Expr to evaluate.} - -\item{df}{If \code{NULL} (default), it evaluates the Expr in an empty DataFrame. -Otherwise, provide a DataFrame that the Expr should be evaluated in.} - -\item{i}{Numeric column to extract. Default is zero (which gives the first -column).} -} -\value{ -R object -} -\description{ -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 -\verb{$to_r()}. -} -\examples{ -pl$expr_to_r(pl$lit(1:3)) -} diff --git a/man/pl_pl.Rd b/man/pl_pl.Rd index a3ffcfd66..ed7dda13a 100644 --- a/man/pl_pl.Rd +++ b/man/pl_pl.Rd @@ -6,7 +6,7 @@ \alias{pl} \title{The complete polars public API.} \format{ -An object of class \code{pl_polars_env} (inherits from \code{environment}) of length 99. +An object of class \code{pl_polars_env} (inherits from \code{environment}) of length 98. } \usage{ pl diff --git a/tests/testthat/_snaps/after-wrappers.md b/tests/testthat/_snaps/after-wrappers.md index 7b9e722f8..c20fcf90c 100644 --- a/tests/testthat/_snaps/after-wrappers.md +++ b/tests/testthat/_snaps/after-wrappers.md @@ -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" --- @@ -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" --- diff --git a/tests/testthat/test-expr_expr.R b/tests/testthat/test-expr_expr.R index 5aea9fa31..08f14e7f9 100644 --- a/tests/testthat/test-expr_expr.R +++ b/tests/testthat/test-expr_expr.R @@ -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) diff --git a/tests/testthat/test-series.R b/tests/testthat/test-series.R index 0d88b63ce..9a81390c2 100644 --- a/tests/testthat/test-series.R +++ b/tests/testthat/test-series.R @@ -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 @@ -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(