Skip to content

Commit

Permalink
fix: Properly slice validity mask on pl.Object series (#18631)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp authored Sep 9, 2024
1 parent 4d176e0 commit 72d861e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion crates/polars-core/src/chunked_array/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ where

unsafe fn slice_unchecked(&mut self, offset: usize, length: usize) {
let len = std::cmp::min(self.len - offset, length);

self.null_bitmap = self
.null_bitmap
.take()
.map(|bitmap| bitmap.sliced_unchecked(offset, length))
.filter(|bitmap| bitmap.unset_bits() > 0);
self.len = len;
self.offset = offset;
}
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/datatypes/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import polars as pl
from polars.exceptions import ComputeError
from polars.testing import assert_series_equal


def test_series_init_instantiated_object() -> None:
Expand Down Expand Up @@ -190,3 +191,11 @@ def test_raise_list_object() -> None:
# We don't want to support this. Unsafe enough as it is already.
with pytest.raises(ValueError):
pl.Series([[object()]], dtype=pl.List(pl.Object()))


def test_object_null_slice() -> None:
s = pl.Series("x", [1, None, 42], dtype=pl.Object)
assert_series_equal(s.is_null(), pl.Series("x", [False, True, False]))
assert_series_equal(s.slice(0, 2).is_null(), pl.Series("x", [False, True]))
assert_series_equal(s.slice(1, 1).is_null(), pl.Series("x", [True]))
assert_series_equal(s.slice(2, 1).is_null(), pl.Series("x", [False]))

0 comments on commit 72d861e

Please sign in to comment.