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()])