From 098e7c1d234a782958a53bc6ac96cb69d34826ab Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Tue, 26 Mar 2024 17:55:36 -0400 Subject: [PATCH 1/9] Add clear for obj --- crates/polars-core/src/series/mod.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index b9fbcb179177..24961e56797f 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -157,17 +157,19 @@ 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) - }; + match self.dtype() { + #[cfg(feature = "object")] + DataType::Object(_, reg) => { + if self.is_empty() { + self.clone() + } else { + let reg = reg.as_ref().unwrap(); + let mut builder = (*reg.builder_constructor)(&self.name(), 0); + builder.to_series() + } + }, + dt => Series::new_empty(self.name(), dt), } - Series::new_empty(self.name(), self.dtype()) } #[doc(hidden)] From 5ba3a3b9f8e544ab55f39f4ee09811cfff003ad1 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Tue, 26 Mar 2024 17:59:34 -0400 Subject: [PATCH 2/9] Remove skip on unit test --- py-polars/tests/unit/operations/test_clear.py | 1 - 1 file changed, 1 deletion(-) 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()]) From 26ce7d56eec7356efc95c84e7f057048d368b199 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Tue, 26 Mar 2024 18:10:22 -0400 Subject: [PATCH 3/9] lint --- crates/polars-core/src/series/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index 24961e56797f..1c83700573d3 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -164,7 +164,7 @@ impl Series { self.clone() } else { let reg = reg.as_ref().unwrap(); - let mut builder = (*reg.builder_constructor)(&self.name(), 0); + let mut builder = (*reg.builder_constructor)(self.name(), 0); builder.to_series() } }, From fb9f6f5d2e91eaf4f667287c9b183aee18f80065 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Tue, 26 Mar 2024 20:42:14 -0400 Subject: [PATCH 4/9] Use slice --- crates/polars-core/src/series/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index 1c83700573d3..f9c22c4650f4 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -159,13 +159,12 @@ impl Series { pub fn clear(&self) -> Series { match self.dtype() { #[cfg(feature = "object")] - DataType::Object(_, reg) => { + DataType::Object(_, _) => { if self.is_empty() { self.clone() } else { - let reg = reg.as_ref().unwrap(); - let mut builder = (*reg.builder_constructor)(self.name(), 0); - builder.to_series() + // SAFETY: we can always take zero elements + unsafe { self.take_slice_unchecked(&[]).clone() } } }, dt => Series::new_empty(self.name(), dt), From 8f351300837a0a1d948c68705a234475c404a6e4 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Wed, 27 Mar 2024 10:20:52 -0400 Subject: [PATCH 5/9] Simplify --- crates/polars-core/src/series/mod.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index f9c22c4650f4..8587cba9ffc2 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -157,17 +157,14 @@ impl Series { } pub fn clear(&self) -> Series { - match self.dtype() { - #[cfg(feature = "object")] - DataType::Object(_, _) => { - if self.is_empty() { - self.clone() - } else { - // SAFETY: we can always take zero elements - unsafe { self.take_slice_unchecked(&[]).clone() } - } - }, - dt => Series::new_empty(self.name(), dt), + if self.is_empty() { + self.clone() + } else { + match self.dtype() { + #[cfg(feature = "object")] + DataType::Object(_, _) => self.take_slice(&[]).unwrap(), + dt => Series::new_empty(self.name(), dt), + } } } From c860e97854f3cb11457dcaa62cccf0e5f04978c6 Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Wed, 27 Mar 2024 13:25:07 -0400 Subject: [PATCH 6/9] Move clone back into obj logic --- crates/polars-core/src/series/mod.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index 8587cba9ffc2..6b0947df5395 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -157,14 +157,16 @@ impl Series { } pub fn clear(&self) -> Series { - if self.is_empty() { - self.clone() - } else { - match self.dtype() { - #[cfg(feature = "object")] - DataType::Object(_, _) => self.take_slice(&[]).unwrap(), - dt => Series::new_empty(self.name(), dt), - } + match self.dtype() { + #[cfg(feature = "object")] + DataType::Object(_, _) => { + if self.is_empty() { + self.clone() + } else { + self.slice(0, 0) + } + }, + dt => Series::new_empty(self.name(), dt), } } From 33a3100482be83fadcef23b5cd4cf9a77b7909ef Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Thu, 28 Mar 2024 09:12:53 -0400 Subject: [PATCH 7/9] Use take --- crates/polars-core/src/series/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index 6b0947df5395..61f8d09a52b2 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -163,7 +163,7 @@ impl Series { if self.is_empty() { self.clone() } else { - self.slice(0, 0) + self.take(&UInt32Chunked::new_vec("", vec![])).unwrap() } }, dt => Series::new_empty(self.name(), dt), From b0a7a0bc86bb547426b97dd571ad0557610eb38a Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Thu, 28 Mar 2024 09:53:55 -0400 Subject: [PATCH 8/9] Use take --- crates/polars-core/src/series/mod.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index 61f8d09a52b2..b9342e5eab2a 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -157,16 +157,14 @@ impl Series { } pub fn clear(&self) -> Series { - match self.dtype() { - #[cfg(feature = "object")] - DataType::Object(_, _) => { - if self.is_empty() { - self.clone() - } else { - self.take(&UInt32Chunked::new_vec("", vec![])).unwrap() - } - }, - dt => Series::new_empty(self.name(), dt), + if self.is_empty() { + self.clone() + } else { + match self.dtype() { + #[cfg(feature = "object")] + DataType::Object(_, _) => self.take(&UInt32Chunked::new_vec("", vec![])).unwrap(), + dt => Series::new_empty(self.name(), dt), + } } } From 51345cbc7b7a11514ace84ffb0ea7b912711bd1c Mon Sep 17 00:00:00 2001 From: Marshall Crumiller Date: Thu, 28 Mar 2024 10:26:01 -0400 Subject: [PATCH 9/9] Use IdxType --- crates/polars-core/src/series/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index b9342e5eab2a..b72a83b4a3c9 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -162,7 +162,9 @@ impl Series { } else { match self.dtype() { #[cfg(feature = "object")] - DataType::Object(_, _) => self.take(&UInt32Chunked::new_vec("", vec![])).unwrap(), + DataType::Object(_, _) => self + .take(&ChunkedArray::::new_vec("", vec![])) + .unwrap(), dt => Series::new_empty(self.name(), dt), } }