Skip to content

Commit

Permalink
fix(rust, python): fix boolean arg-max if all equal (#5680)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 30, 2022
1 parent 264da15 commit ef53b62
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
20 changes: 16 additions & 4 deletions polars/polars-core/src/chunked_array/ops/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,12 +848,24 @@ where

impl ArgAgg for BooleanChunked {
fn arg_min(&self) -> Option<usize> {
self.into_iter()
.position(|opt_val| matches!(opt_val, Some(false)))
if self.is_empty() || self.null_count() == self.len() {
None
} else if self.all() {
Some(0)
} else {
self.into_iter()
.position(|opt_val| matches!(opt_val, Some(false)))
}
}
fn arg_max(&self) -> Option<usize> {
self.into_iter()
.position(|opt_val| matches!(opt_val, Some(true)))
if self.is_empty() || self.null_count() == self.len() {
None
} else if self.any() {
self.into_iter()
.position(|opt_val| matches!(opt_val, Some(true)))
} else {
Some(0)
}
}
}
impl ArgAgg for Utf8Chunked {}
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,6 +1610,12 @@ def test_arg_min_and_arg_max() -> None:
s = pl.Series([None, None], dtype=pl.Boolean)
assert s.arg_min() is None
assert s.arg_max() is None
s = pl.Series([True, True])
assert s.arg_min() == 0
assert s.arg_max() == 0
s = pl.Series([False, False])
assert s.arg_min() == 0
assert s.arg_max() == 0


def test_is_null_is_not_null() -> None:
Expand Down

0 comments on commit ef53b62

Please sign in to comment.