Skip to content

Commit

Permalink
fix: Keeps Enum order when converting to R (#1252)
Browse files Browse the repository at this point in the history
Co-authored-by: etiennebacher <etienne.bacher@protonmail.com>
Co-authored-by: Etienne Bacher <52219252+etiennebacher@users.noreply.github.com>
Co-authored-by: eitsupi <50911393+eitsupi@users.noreply.github.com>
Co-authored-by: eitsupi <ts1s1andn@gmail.com>
  • Loading branch information
5 people authored Oct 19, 2024
1 parent 0ef5a4c commit 206d6a4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
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_(),
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.

0 comments on commit 206d6a4

Please sign in to comment.