Skip to content

Commit

Permalink
Merge branch 'main' into vignette-diff-python
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Apr 10, 2024
2 parents 9e06edf + 9f4a594 commit 84195c7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ S3method(as_polars_series,clock_time_point)
S3method(as_polars_series,clock_zoned_time)
S3method(as_polars_series,data.frame)
S3method(as_polars_series,default)
S3method(as_polars_series,list)
S3method(as_polars_series,nanoarrow_array)
S3method(as_polars_series,nanoarrow_array_stream)
S3method(as_polars_series,rpolars_raw_list)
S3method(as_polars_series,vctrs_rcrd)
S3method(c,RPolarsSeries)
S3method(dim,RPolarsDataFrame)
Expand Down
30 changes: 30 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,36 @@

### Other breaking changes

- R objects inside an R list are now converted to Polars data types via
`as_polars_series()` (#1021). For example, up to polars 0.15.1,
data.frames inside a list were converted to a nested List type:

```r
pl$select(nested_data = pl$lit(list(data.frame(a = 1))))
#> shape: (1, 1)
#> ┌─────────────────┐
#> │ nested_data │
#> │ --- │
#> │ list[list[f64]] │
#> ╞═════════════════╡
#> │ [[1.0]] │
#> └─────────────────┘
```

From 0.16.0, data.frames inside a list are converted to the polars Struct type:

```r
pl$select(nested_data = pl$lit(list(data.frame(a = 1))))
#> shape: (1, 1)
#> ┌─────────────────┐
#> │ nested_data │
#> │ --- │
#> │ list[struct[1]] │
#> ╞═════════════════╡
#> │ [{1.0}] │
#> └─────────────────┘
```

- Several functions have been rewritten to match the behavior of Python Polars.

- In `pl$Series()` arguments are changed.
Expand Down
22 changes: 22 additions & 0 deletions R/as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,25 @@ as_polars_series.clock_zoned_time = function(x, name = NULL, ...) {
...
)$dt$replace_time_zone(time_zone)
}


# TODO: rewrite `recursive_robjname2series_tree` in Rust side
#' @rdname as_polars_series
#' @export
as_polars_series.list = function(x, name = NULL, ...) {
lapply(x, \(child) {
if (is.null(child)) {
NULL # if `NULL`, the type will be resolved later
} else {
as_polars_series(child)
}
}) |>
as_polars_series.default(name = name)
}


# TODO: reconsider `rpolars_raw_list`
#' @export
as_polars_series.rpolars_raw_list = function(x, name = NULL, ...) {
as_polars_series.default(x, name = name)
}
3 changes: 3 additions & 0 deletions man/as_polars_series.Rd

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

13 changes: 13 additions & 0 deletions tests/testthat/test-as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,16 @@ test_that("clock_zoned_time may returns empty time zone", {

expect_s3_class(as_polars_series(clock::zoned_time_now(zone = "")), "RPolarsSeries")
})


test_that("as_polars_series for nested type", {
expect_true(
as_polars_series(list(list(data.frame(a = 1))))$dtype == pl$List(pl$List(pl$Struct(a = pl$Float64)))
)

# TODO: this shouldn't error
expect_grepl_error(
as_polars_series(list(as_polars_series(NULL), as_polars_series(1L))),
"One element was null and another was i32"
)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-expr_list.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test_that("list$len", {
df = pl$DataFrame(list_of_strs = as_polars_series(list(c("a", "b"), "c", character(), list(), NULL)))
df = pl$DataFrame(list_of_strs = as_polars_series(list(c("a", "b"), "c", character(), NULL, NULL)))
l = df$with_columns(pl$col("list_of_strs")$list$len()$alias("list_of_strs_lengths"))$to_list()

expect_identical(
Expand Down

0 comments on commit 84195c7

Please sign in to comment.