diff --git a/DESCRIPTION b/DESCRIPTION index 4b697fa11..d6b2a434b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,6 +35,7 @@ Suggests: nanoarrow (>= 0.6.0), nycflights13, patrick, + quickcheck, pillar, rlang, rmarkdown, @@ -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 diff --git a/NEWS.md b/NEWS.md index bdaf8b2a1..7d8dc994a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). diff --git a/src/rust/Cargo.lock b/src/rust/Cargo.lock index 86cb8c733..3b33c0f53 100644 --- a/src/rust/Cargo.lock +++ b/src/rust/Cargo.lock @@ -2277,7 +2277,7 @@ dependencies = [ [[package]] name = "r-polars" -version = "0.43.0" +version = "0.43.1" dependencies = [ "either", "extendr-api", diff --git a/src/rust/Cargo.toml b/src/rust/Cargo.toml index 0c115bc95..494c96413 100644 --- a/src/rust/Cargo.toml +++ b/src/rust/Cargo.toml @@ -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 diff --git a/src/rust/src/conversion_s_to_r.rs b/src/rust/src/conversion_s_to_r.rs index 2963b5b91..e32885899 100644 --- a/src/rust/src/conversion_s_to_r.rs +++ b/src/rust/src/conversion_s_to_r.rs @@ -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()), diff --git a/tests/testthat/test-datatype.R b/tests/testthat/test-datatype.R index 3066ce4f2..e023c4232 100644 --- a/tests/testthat/test-datatype.R +++ b/tests/testthat/test-datatype.R @@ -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") diff --git a/tools/lib-sums.tsv b/tools/lib-sums.tsv deleted file mode 100644 index 8fe98f387..000000000 --- a/tools/lib-sums.tsv +++ /dev/null @@ -1,6 +0,0 @@ -url sha256sum -https://github.com/pola-rs/r-polars/releases/download/lib-v0.43.0/libr_polars-0.43.0-aarch64-apple-darwin.tar.gz 6b81c785078894eba575d5c53210afacd47cd4f5a346dc74958d500d34b4cb84 -https://github.com/pola-rs/r-polars/releases/download/lib-v0.43.0/libr_polars-0.43.0-aarch64-unknown-linux-gnu.tar.gz cc727b05816fac13e8223e4f23077ef931c2351d1b20ac8848e70bf52d9f7d34 -https://github.com/pola-rs/r-polars/releases/download/lib-v0.43.0/libr_polars-0.43.0-x86_64-apple-darwin.tar.gz 5d23f27814fb3de60783acaad8594897f60f16f4b7ec94344f4bbaffaad5cca6 -https://github.com/pola-rs/r-polars/releases/download/lib-v0.43.0/libr_polars-0.43.0-x86_64-pc-windows-gnu.tar.gz 5b1120f2cda482ee734f3952eb6c3aa2c56286280c109cdf897a9eb3bcddae4f -https://github.com/pola-rs/r-polars/releases/download/lib-v0.43.0/libr_polars-0.43.0-x86_64-unknown-linux-gnu.tar.gz 0831fa1beed3847008eaa8f993199e895c68c9fc548696574dafd77ca431259e