Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: fix list type column conversion from data frame #1022

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,41 @@
### 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:
`as_polars_series()` (#1021, #1022). For example, up to polars 0.15.1,
a list containing a data.frame with a column of `{clock}` naive-time class
was converted to a nested List type of Float64:

```r
pl$select(nested_data = pl$lit(list(data.frame(a = 1))))
data = data.frame(time = clock::naive_time_parse("1990-01-01", precision = "day"))
pl$select(
nested_data = pl$lit(list(data))
)
#> shape: (1, 1)
#> ┌─────────────────┐
#> │ nested_data │
#> │ --- │
#> │ list[list[f64]] │
#> ╞═════════════════╡
#> │ [[1.0]]
#> └─────────────────┘
#> ┌──────────────────────────
#> │ nested_data
#> │ ---
#> │ list[list[list[f64]]]
#> ╞══════════════════════════
#> │ [[[2.1475e9], [7305.0]]]
#> └──────────────────────────
```

From 0.16.0, data.frames inside a list are converted to the polars Struct type:
From 0.16.0, nested types are correctly converted, so that will be
a List type of Struct type containing a Datetime type.

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

- Several functions have been rewritten to match the behavior of Python Polars.
Expand Down
2 changes: 1 addition & 1 deletion R/construction.R
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ df_to_rpldf = function(x, ..., schema = NULL, schema_overrides = NULL) {
unwrap()
}

out = lapply(x, as_polars_series) |>
out = lapply(x, \(col) as_polars_series(unAsIs(col))) |>
pl$select()

out$columns = col_names
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ 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)))
)
expect_true(
as_polars_series(
list(data.frame(a = I(list(data.frame(b = 1L)))))
)$dtype == pl$List(pl$Struct(a = pl$List(pl$Struct(b = pl$Int32))))
)

# TODO: this shouldn't error
expect_grepl_error(
Expand Down
Loading