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: Keeps Enum order when converting to R #1252

Merged
merged 11 commits into from
Oct 19, 2024
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Suggests:
nanoarrow (>= 0.6.0),
nycflights13,
patrick,
quickcheck,
pillar,
rlang,
rmarkdown,
Expand Down Expand Up @@ -118,5 +119,5 @@ Collate:
'zzz.R'
Config/rextendr/version: 0.3.1
VignetteBuilder: knitr
Config/polars/LibVersion: 0.43.0
Config/polars/LibVersion: 0.43.1
Config/polars/RustToolchainVersion: nightly-2024-09-19
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Polars R Package (development version)

### Bug fixes

- Maintain level order when converting Enums to factors (#1252, @andyquinterom).

## Polars R Package 0.20.0

- Updated rust-polars to 0.43.1 (#1230).
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.

2 changes: 1 addition & 1 deletion src/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "r-polars"
version = "0.43.0"
version = "0.43.1"
edition = "2021"
rust-version = "1.80.0"
publish = false
Expand Down
8 changes: 5 additions & 3 deletions src/rust/src/conversion_s_to_r.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ pub fn pl_series_to_list(
.set_class(["rpolars_raw_list", "list"])
.expect("this class label is always valid")
}),
Enum(_, _) => s
.categorical()
.map(|ca| extendr_api::call!("factor", ca.iter_str().collect_robj()).unwrap()),
Enum(_, _) => s.categorical().map(|ca| {
let levels = ca.unique().unwrap().iter_str().collect_robj();
let values = ca.iter_str().collect_robj();
extendr_api::call!("factor", values, levels).unwrap()
}),
Categorical(_, _) => s
.categorical()
.map(|ca| extendr_api::call!("factor", ca.iter_str().collect_robj()).unwrap()),
Expand Down
15 changes: 10 additions & 5 deletions tests/testthat/test-datatype.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,16 @@ test_that("contains_* functions for datatype work", {
})

test_that("Enum", {
expect_identical(
as_polars_series(c("z", "z", "k", "a"))$
cast(pl$Enum(c("z", "k", "a")))$
to_r(),
factor(c("z", "z", "k", "a"))
quickcheck::for_all(
factors = quickcheck::factor_(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about including NA?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but there is an issue about that.
armcn/quickcheck#22

property = function(factors) {
expect_identical(
as_polars_series(as.character(factors))$
cast(pl$Enum(levels(factors)))$
to_r(),
factors
)
}
)

expect_grepl_error(pl$Enum(), "missing")
Expand Down
6 changes: 0 additions & 6 deletions tools/lib-sums.tsv

This file was deleted.

Loading