Skip to content

Commit

Permalink
fix: do not error when converting datatype Null to R (#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Sep 1, 2024
1 parent 383c85e commit 26c60ce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
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

0 comments on commit 26c60ce

Please sign in to comment.