diff --git a/crates/polars-core/src/chunked_array/object/mod.rs b/crates/polars-core/src/chunked_array/object/mod.rs index 1b018800dd98..88b3d84e726f 100644 --- a/crates/polars-core/src/chunked_array/object/mod.rs +++ b/crates/polars-core/src/chunked_array/object/mod.rs @@ -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; } diff --git a/py-polars/tests/unit/datatypes/test_object.py b/py-polars/tests/unit/datatypes/test_object.py index 803e7933b8ab..8db373d3f58a 100644 --- a/py-polars/tests/unit/datatypes/test_object.py +++ b/py-polars/tests/unit/datatypes/test_object.py @@ -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: @@ -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]))