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

feat: as_polars_df() keeps the schema when converting a `nanoarrow_… #1177

Merged
merged 1 commit into from
Aug 8, 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
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"))
})
Loading