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: do not error when converting datatype Null to R #1217

Merged
merged 3 commits into from
Sep 1, 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
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

## Polars R Package (development version)

### Bug fixes

- Converting data of datatype `Null` to R doesn't error anymore. It now creates
a column filled with `NA` (#1217).

## Polars R Package 0.19.1

- This is a maintenance relase. No user facing changes.
- This is a maintenance release. No user facing changes.

## Polars R Package 0.19.0

Expand Down
14 changes: 12 additions & 2 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,19 @@ DataFrame_group_by = function(..., maintain_order = polars_options()$maintain_or
#' df$to_data_frame()
DataFrame_to_data_frame = function(..., int64_conversion = polars_options()$int64_conversion) {
# do not unnest structs and mark with I to also preserve categoricals as is
l = lapply(self$to_list(unnest_structs = FALSE, int64_conversion = int64_conversion), I)
l = lapply(
self$to_list(unnest_structs = FALSE, int64_conversion = int64_conversion),
function(x) {
# correctly handle columns with datatype Null
if (is.null(x)) {
NA
} else {
I(x)
}
}
)

# similar to as.data.frame, but avoid checks, whcih would edit structs
# similar to as.data.frame, but avoid checks, which would edit structs
df = data.frame(seq_along(l[[1L]]), ...)
for (i in seq_along(l)) df[[i]] = l[[i]]
names(df) = .pr$DataFrame$columns(self)
Expand Down
2 changes: 1 addition & 1 deletion src/rust/Cargo.lock

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

17 changes: 17 additions & 0 deletions tests/testthat/test-dataframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,23 @@ test_that("to_Struct, unnest, to_frame, to_data_frame", {
expect_identical(df$to_data_frame(), df_e)
})

test_that("conversion of datatype Null to R works", {
df = pl$DataFrame(x = NULL)
expect_identical(
df$to_data_frame(),
data.frame(x = NA)
)

df2 = pl$DataFrame(x = 1:2, y = NULL)
expect_identical(
df2$to_data_frame(),
data.frame(x = 1:2, y = c(NA, NA))
)
expect_identical(
as.data.frame(df2),
data.frame(x = 1:2, y = c(NA, NA))
)
})

test_that("unnest works correctly", {
df = pl$DataFrame(
Expand Down
Loading