From 3888b8665d150402b48543374eef71615a77e5d1 Mon Sep 17 00:00:00 2001 From: Marshall Date: Thu, 28 Mar 2024 11:08:17 -0400 Subject: [PATCH] fix: Return correct dtype for `s.clear()` when dtype is `Object` (#15315) --- crates/polars-core/src/series/mod.rs | 20 +++++++++---------- py-polars/tests/unit/operations/test_clear.py | 1 - 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index b9fbcb179177..b72a83b4a3c9 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -157,17 +157,17 @@ impl Series { } pub fn clear(&self) -> Series { - // Only the inner of objects know their type, so use this hack. - #[cfg(feature = "object")] - if matches!(self.dtype(), DataType::Object(_, _)) { - return if self.is_empty() { - self.clone() - } else { - let av = self.get(0).unwrap(); - Series::new(self.name(), [av]).slice(0, 0) - }; + if self.is_empty() { + self.clone() + } else { + match self.dtype() { + #[cfg(feature = "object")] + DataType::Object(_, _) => self + .take(&ChunkedArray::::new_vec("", vec![])) + .unwrap(), + dt => Series::new_empty(self.name(), dt), + } } - Series::new_empty(self.name(), self.dtype()) } #[doc(hidden)] diff --git a/py-polars/tests/unit/operations/test_clear.py b/py-polars/tests/unit/operations/test_clear.py index 76aba9b6e387..0ac3c1d27ba0 100644 --- a/py-polars/tests/unit/operations/test_clear.py +++ b/py-polars/tests/unit/operations/test_clear.py @@ -65,7 +65,6 @@ def test_clear_lf() -> None: assert ldfe.collect().rows() == [(None, None, None), (None, None, None)] -@pytest.mark.skip("Currently bugged: https://github.com/pola-rs/polars/issues/15303") def test_clear_series_object_starting_with_null() -> None: s = pl.Series([None, object()])