From e4dcec535dba9a7228c8ce5f3321475d535f9a9e Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Thu, 23 Jan 2025 03:45:14 -0500 Subject: [PATCH] feat: Add unique operations for Decimal dtype (#20855) --- .../src/series/implementations/decimal.rs | 15 +++++++++++++++ py-polars/tests/unit/datatypes/test_decimal.py | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/polars-core/src/series/implementations/decimal.rs b/crates/polars-core/src/series/implementations/decimal.rs index ad81963ff828..c9052f467cf4 100644 --- a/crates/polars-core/src/series/implementations/decimal.rs +++ b/crates/polars-core/src/series/implementations/decimal.rs @@ -336,6 +336,21 @@ impl SeriesTrait for SeriesWrap { self.0.has_nulls() } + #[cfg(feature = "algorithm_group_by")] + fn unique(&self) -> PolarsResult { + Ok(self.apply_physical_to_s(|ca| ca.unique().unwrap())) + } + + #[cfg(feature = "algorithm_group_by")] + fn n_unique(&self) -> PolarsResult { + self.0.n_unique() + } + + #[cfg(feature = "algorithm_group_by")] + fn arg_unique(&self) -> PolarsResult { + self.0.arg_unique() + } + fn is_null(&self) -> BooleanChunked { self.0.is_null() } diff --git a/py-polars/tests/unit/datatypes/test_decimal.py b/py-polars/tests/unit/datatypes/test_decimal.py index 14cf6564b91a..b1e97abaa41e 100644 --- a/py-polars/tests/unit/datatypes/test_decimal.py +++ b/py-polars/tests/unit/datatypes/test_decimal.py @@ -651,3 +651,11 @@ def test_fill_null() -> None: assert s.fill_null(strategy="forward").to_list() == [D("1.2"), D("1.2"), D("1.4")] assert s.fill_null(strategy="backward").to_list() == [D("1.2"), D("1.4"), D("1.4")] assert s.fill_null(strategy="mean").to_list() == [D("1.2"), D("1.3"), D("1.4")] + + +def test_unique() -> None: + ser = pl.Series([D("1.1"), D("1.1"), D("2.2")]) + + assert ser.unique().to_list() == [D("1.1"), D("2.2")] + assert ser.n_unique() == 2 + assert ser.arg_unique().to_list() == [0, 2]