Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Return correct dtype for s.clear() when dtype is Object #15315

Merged
merged 10 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions crates/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,18 @@ 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(_, _) => {
if self.is_empty() {
self.clone()
} else {
// SAFETY: we can always take zero elements
unsafe { self.take_slice_unchecked(&[]).clone() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works. Though this is not worth doing unsafe. Can we use self.take.

I also wonder why we need to clone afterwards? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do. Out of curiosity, is this path a bit simpler/faster than the standard dtype path of going through full_null with zero elements? We could just remove all the if logic and just return an empty slice for all dtypes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh unfortunately that breaks pl.Array. I'll keep the special object logic in there.

}
},
dt => Series::new_empty(self.name(), dt),
}
Series::new_empty(self.name(), self.dtype())
}

#[doc(hidden)]
Expand Down
1 change: 0 additions & 1 deletion py-polars/tests/unit/operations/test_clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()])

Expand Down
Loading