diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 0d982e95..524a50be 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,10 @@ Changelog 4.2.2 - unreleased ------------------ +**Bug fixes:** + +- Fixed compatibility with polars 2.0, which removed ``Series.cat.get_categories()``. + **Other changes:** - Clarified that ``sparse_threshold`` is used to sparsify numerical columns and is not used to densify columns that are already sparse. diff --git a/src/tabmat/categorical_matrix.py b/src/tabmat/categorical_matrix.py index 014091a5..86c6ce91 100644 --- a/src/tabmat/categorical_matrix.py +++ b/src/tabmat/categorical_matrix.py @@ -243,14 +243,14 @@ def _extract_codes_and_categories_pandas(cat_vec) -> tuple[np.ndarray, np.ndarra def _extract_codes_and_categories_polars(cat_vec) -> tuple[np.ndarray, np.ndarray]: dtype = cat_vec.dtype if isinstance(dtype, pl.Enum): - categories = cat_vec.cat.get_categories().to_numpy() + categories = dtype.categories.to_numpy() indices = cat_vec.to_physical().fill_null(-1).to_numpy() return indices, categories if not isinstance(cat_vec.dtype, pl.Categorical): cat_vec = cat_vec.cast(pl.Categorical) - # as of polars 1.32, `get_categories()` won't yield a useful result as - # this is "not per column" anymore. + # as of polars 1.32, `Series.cat.get_categories()` won't yield a useful + # result as this is "not per column" anymore (and it is removed in 2.0). mask = cat_vec.is_null() categories = cat_vec.filter(~mask).unique().sort().to_numpy() indices = np.nan_to_num(cat_vec.rank("dense").to_numpy() - 1, nan=-1)