From 245d5498952b0e98ba5759a64ee0d0c06c7f6518 Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Sun, 17 Nov 2024 09:56:21 +0100 Subject: [PATCH] fix: Raise if merge non-global categoricals in unpivot (#19826) --- crates/polars-core/src/utils/supertype.rs | 1 + py-polars/tests/unit/operations/test_unpivot.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/crates/polars-core/src/utils/supertype.rs b/crates/polars-core/src/utils/supertype.rs index 18b8c9ddd00a..4e30a8da7904 100644 --- a/crates/polars-core/src/utils/supertype.rs +++ b/crates/polars-core/src/utils/supertype.rs @@ -524,6 +524,7 @@ pub fn merge_dtypes_many + Clone, D: AsRef>( let DataType::Categorical(Some(rm), _) = first_dt else { unreachable!() }; + polars_ensure!(matches!(rm.as_ref(), RevMapping::Global(_, _, _)), ComputeError: "global string cache must be set to merge categorical columns"); let mut merger = GlobalRevMapMerger::new(rm.clone()); diff --git a/py-polars/tests/unit/operations/test_unpivot.py b/py-polars/tests/unit/operations/test_unpivot.py index 434c2fdc3af9..177548721f2d 100644 --- a/py-polars/tests/unit/operations/test_unpivot.py +++ b/py-polars/tests/unit/operations/test_unpivot.py @@ -113,3 +113,8 @@ def test_unpivot_categorical_global() -> None: "variable": ["1", "1", "2", "2"], "value": ["a", "b", "b", "c"], } + + +def test_unpivot_categorical_raise_19770() -> None: + with pytest.raises(pl.exceptions.ComputeError): + (pl.DataFrame({"x": ["foo"]}).cast(pl.Categorical).unpivot())