Skip to content

Commit

Permalink
feat: as_polars_df() keeps the schema when converting a `nanoarrow_… (
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher authored Aug 8, 2024
1 parent 3d7c2ff commit 5ac02a6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### New features

- New method `$str$extract_many()` (#1163).
- Converting a `nanoarrow_array` with zero rows to an `RPolarsDataFrame` via
`as_polars_df()` now keeps the original schema (#1177).

### Bug fixes

Expand Down
8 changes: 1 addition & 7 deletions R/as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,7 @@ as_polars_df.nanoarrow_array = function(x, ...) {
}

series = as_polars_series.nanoarrow_array(x, name = NULL)

if (length(series)) {
series$to_frame()$unnest("")
} else {
# TODO: support 0-length array
pl$DataFrame()
}
series$to_frame()$unnest("")
}


Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-as_polars.R
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,14 @@ patrick::with_parameters_test_that(
},
.cases = make_as_polars_df_experimental_cases()
)


test_that("as_polars_df works for nanoarrow_array with zero rows", {
skip_if_not_installed("nanoarrow")
orig = data.frame(col1 = character(0), col2 = numeric(0))
out = nanoarrow::as_nanoarrow_array(orig) |>
as_polars_df() |>
as.data.frame()
expect_identical(out, orig)
expect_identical(lapply(out, class), list(col1 = "character", col2 = "numeric"))
})

0 comments on commit 5ac02a6

Please sign in to comment.