From c1805ba9ba5467961f695056c1b40f29d7bbedfc Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Wed, 15 Jan 2025 06:24:07 -0500 Subject: [PATCH] fix: Fix `Series.n_unique` raising for list of struct --- .../polars-core/src/series/implementations/list.rs | 3 --- .../tests/unit/operations/unique/test_n_unique.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/crates/polars-core/src/series/implementations/list.rs b/crates/polars-core/src/series/implementations/list.rs index 74d61d9d91a8..f8ca26e9902d 100644 --- a/crates/polars-core/src/series/implementations/list.rs +++ b/crates/polars-core/src/series/implementations/list.rs @@ -200,9 +200,6 @@ impl SeriesTrait for SeriesWrap { #[cfg(feature = "algorithm_group_by")] fn n_unique(&self) -> PolarsResult { - if !self.inner_dtype().is_primitive_numeric() { - polars_bail!(opq = n_unique, self.dtype()); - } // this can be called in aggregation, so this fast path can be worth a lot match self.len() { 0 => Ok(0), diff --git a/py-polars/tests/unit/operations/unique/test_n_unique.py b/py-polars/tests/unit/operations/unique/test_n_unique.py index c221ae23b377..34e441ce42be 100644 --- a/py-polars/tests/unit/operations/unique/test_n_unique.py +++ b/py-polars/tests/unit/operations/unique/test_n_unique.py @@ -46,3 +46,17 @@ def test_n_unique_null() -> None: ) def test_n_unique_categorical(input: list[str | None], output: int) -> None: assert pl.Series(input, dtype=pl.Categorical).n_unique() == output + + +def test_n_unique_list_of_struct_20341() -> None: + df = pl.DataFrame( + { + "a": [ + [{"a": 1, "b": 2}, {"a": 10, "b": 20}], + [{"a": 1, "b": 2}, {"a": 10, "b": 20}], + [{"a": 3, "b": 4}], + ] + } + ) + assert df.select("a").n_unique() == 2 + assert df["a"].n_unique() == 2