diff --git a/NEWS.md b/NEWS.md index 7489aa0ee..ad425bf23 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/dataframe__frame.R b/R/dataframe__frame.R index fab5fd769..3fa753c39 100644 --- a/R/dataframe__frame.R +++ b/R/dataframe__frame.R @@ -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) diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index aa3c25af6..007d285f0 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -2238,7 +2238,7 @@ dependencies = [ [[package]] name = "r-polars" -version = "0.42.0" +version = "0.42.1" dependencies = [ "either", "extendr-api", diff --git a/tests/testthat/test-dataframe.R b/tests/testthat/test-dataframe.R index 23bffcf30..b61c94ff9 100644 --- a/tests/testthat/test-dataframe.R +++ b/tests/testthat/test-dataframe.R @@ -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(