Skip to content

Commit

Permalink
feat(rust, python): add arg_min/arg_max for series of dtype boolean (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 22, 2022
1 parent 664a851 commit 52487c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion polars/polars-core/src/chunked_array/ops/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,16 @@ where
}
}

impl ArgAgg for BooleanChunked {}
impl ArgAgg for BooleanChunked {
fn arg_min(&self) -> Option<usize> {
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)))
}
}
impl ArgAgg for Utf8Chunked {}
#[cfg(feature = "dtype-binary")]
impl ArgAgg for BinaryChunked {}
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/unit/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,13 @@ def test_arg_min_and_arg_max() -> None:
assert s.arg_min() == 3
assert s.arg_max() == 0

s = pl.Series([None, True, False, True])
assert s.arg_min() == 2
assert s.arg_max() == 1
s = pl.Series([None, None], dtype=pl.Boolean)
assert s.arg_min() is None
assert s.arg_max() is None


def test_is_null_is_not_null() -> None:
s = pl.Series("a", [1.0, 2.0, 3.0, None])
Expand Down

0 comments on commit 52487c2

Please sign in to comment.