From c5eee44a9a43734356f90b2f9dff132d96777862 Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Mon, 9 Sep 2024 16:06:16 +0200 Subject: [PATCH 1/3] fix: Properly slice validity mask on pl.Object series --- crates/polars-core/src/chunked_array/object/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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; } From 916c9329826bb3d54c99611f6381cf50a71c42d5 Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Mon, 9 Sep 2024 16:09:46 +0200 Subject: [PATCH 2/3] add test --- py-polars/tests/unit/datatypes/test_object.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/py-polars/tests/unit/datatypes/test_object.py b/py-polars/tests/unit/datatypes/test_object.py index 803e7933b8ab..9991ee8a6959 100644 --- a/py-polars/tests/unit/datatypes/test_object.py +++ b/py-polars/tests/unit/datatypes/test_object.py @@ -190,3 +190,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 s.is_null() == [False, True, False] + assert s.slice(0, 2).is_null() == [False, True] + assert s.slice(1, 1).is_null() == [True] + assert s.slice(2, 1).is_null() == [False] \ No newline at end of file From ea72809300d1f33e8b6a5b4a7cf535ddc253953f Mon Sep 17 00:00:00 2001 From: Orson Peters Date: Mon, 9 Sep 2024 16:12:04 +0200 Subject: [PATCH 3/3] fix test --- py-polars/tests/unit/datatypes/test_object.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/py-polars/tests/unit/datatypes/test_object.py b/py-polars/tests/unit/datatypes/test_object.py index 9991ee8a6959..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: @@ -194,7 +195,7 @@ def test_raise_list_object() -> None: def test_object_null_slice() -> None: s = pl.Series("x", [1, None, 42], dtype=pl.Object) - assert s.is_null() == [False, True, False] - assert s.slice(0, 2).is_null() == [False, True] - assert s.slice(1, 1).is_null() == [True] - assert s.slice(2, 1).is_null() == [False] \ No newline at end of file + 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]))