From 0a3070942529747a8c30f996a5a4ee3aa6d455d2 Mon Sep 17 00:00:00 2001 From: ritchie Date: Thu, 12 Sep 2024 12:59:59 +0200 Subject: [PATCH 1/2] fix: Fix categorical --- pyo3-polars/src/types.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyo3-polars/src/types.rs b/pyo3-polars/src/types.rs index 8fe0752..af4cfd6 100644 --- a/pyo3-polars/src/types.rs +++ b/pyo3-polars/src/types.rs @@ -2,8 +2,6 @@ use super::*; use crate::error::PyPolarsErr; use crate::ffi::to_py::to_py_array; use polars::export::arrow; -#[cfg(feature = "dtype-categorical")] -use polars_core::datatypes::create_enum_data_type; use polars_core::datatypes::{CompatLevel, DataType}; use polars_core::prelude::*; use polars_core::utils::materialize_dyn_int; @@ -592,7 +590,7 @@ impl<'py> FromPyObject<'py> for PyDataType { let s = get_series(&categories.as_borrowed())?; let ca = s.str().map_err(PyPolarsErr::from)?; let categories = ca.downcast_iter().next().unwrap().clone(); - create_enum_data_type(categories) + DataType::Enum(Some(RevMapping::build_local(categories)), Default::default()) }, "Date" => DataType::Date, "Time" => DataType::Time, From b7b9895cba7c6109a140cdfb9d0b8806d9c4a544 Mon Sep 17 00:00:00 2001 From: ritchie Date: Thu, 12 Sep 2024 13:06:18 +0200 Subject: [PATCH 2/2] fix: Fix categorical --- pyo3-polars/src/types.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pyo3-polars/src/types.rs b/pyo3-polars/src/types.rs index af4cfd6..743d0c4 100644 --- a/pyo3-polars/src/types.rs +++ b/pyo3-polars/src/types.rs @@ -341,7 +341,7 @@ impl IntoPy for PyExpr { } } -#[cfg(feature = "dtype-full")] +#[cfg(feature = "dtype-categorical")] pub(crate) fn to_series(py: Python, s: PySeries) -> PyObject { let series = SERIES.bind(py); let constructor = series @@ -573,11 +573,12 @@ impl<'py> FromPyObject<'py> for PyDataType { #[cfg(feature = "dtype-categorical")] "Categorical" => { let ordering = ob.getattr(intern!(py, "ordering")).unwrap(); - - let ordering = match ordering.extract::<&str>()? { - "physical" => CategoricalOrdering::Physical, - "lexical" => CategoricalOrdering::Lexical, + let ordering = ordering.extract::()?; + let ordering = match ordering.as_bytes() { + b"physical" => CategoricalOrdering::Physical, + b"lexical" => CategoricalOrdering::Lexical, ordering => { + let ordering = std::str::from_utf8(ordering).unwrap(); return Err(PyValueError::new_err(format!("invalid ordering argument: {ordering}"))) } }; @@ -590,7 +591,7 @@ impl<'py> FromPyObject<'py> for PyDataType { let s = get_series(&categories.as_borrowed())?; let ca = s.str().map_err(PyPolarsErr::from)?; let categories = ca.downcast_iter().next().unwrap().clone(); - DataType::Enum(Some(RevMapping::build_local(categories)), Default::default()) + DataType::Enum(Some(Arc::new(RevMapping::build_local(categories))), Default::default()) }, "Date" => DataType::Date, "Time" => DataType::Time,