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: list.min/max with empty and/or None elements #14018

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions crates/polars-ops/src/chunked_array/list/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ where
.map(|end| {
let current_offset = running_offset;
running_offset = *end;
if current_offset == *end {
reswqa marked this conversation as resolved.
Show resolved Hide resolved
return None;
}

let slice = unsafe { values.get_unchecked(current_offset as usize..*end as usize) };
slice.min_ignore_nan_kernel()
Expand Down Expand Up @@ -122,6 +125,9 @@ where
.map(|end| {
let current_offset = running_offset;
running_offset = *end;
if current_offset == *end {
return None;
}

let slice = unsafe { values.get_unchecked(current_offset as usize..*end as usize) };
slice.max_ignore_nan_kernel()
Expand Down
29 changes: 29 additions & 0 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,35 @@ def test_list_min_max() -> None:
}


def test_list_min_max_13978() -> None:
df = pl.DataFrame(
{
"a": [[], [1, 2, 3]],
"b": [[1, 2], None],
"c": [[], [None, 1, 2]],
}
)
out = df.select(
min_a=pl.col("a").list.min(),
max_a=pl.col("a").list.max(),
min_b=pl.col("b").list.min(),
max_b=pl.col("b").list.max(),
min_c=pl.col("c").list.min(),
max_c=pl.col("c").list.max(),
)
expected = pl.DataFrame(
{
"min_a": [None, 1],
"max_a": [None, 3],
"min_b": [1, None],
"max_b": [2, None],
"min_c": [None, 1],
"max_c": [None, 2],
}
)
assert_frame_equal(out, expected)


def test_fill_null_empty_list() -> None:
assert pl.Series([["a"], None]).fill_null([]).to_list() == [["a"], []]

Expand Down
Loading