Skip to content

Commit

Permalink
fix: Fix incorrect reverse on struct containing NULLs (#19446)
Browse files Browse the repository at this point in the history
  • Loading branch information
nameexhaustion authored Oct 25, 2024
1 parent 25b54dc commit 34b1dc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion crates/polars-core/src/series/implementations/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ impl SeriesTrait for SeriesWrap<StructChunked> {
}

fn reverse(&self) -> Series {
self.0._apply_fields(|s| s.reverse()).unwrap().into_series()
let validity = self
.rechunk_validity()
.map(|x| x.into_iter().rev().collect::<Bitmap>());
self.0
._apply_fields(|s| s.reverse())
.unwrap()
.with_outer_validity(validity)
.into_series()
}

fn shift(&self, periods: i64) -> Series {
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,3 +1149,10 @@ def test_list_to_struct_19208() -> None:
).to_dict(as_series=False) == {
"nested": [{"field_0": {"a": 1}}, {"field_0": None}, {"field_0": {"a": 3}}]
}


def test_struct_reverse_outer_validity_19445() -> None:
assert_series_equal(
pl.Series([{"a": 1}, None]).reverse(),
pl.Series([None, {"a": 1}]),
)

0 comments on commit 34b1dc5

Please sign in to comment.