Skip to content

Commit

Permalink
feat[rust, python]: allow min/max on utf8 dtype
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 1, 2022
1 parent 33a1c27 commit 6a0e78c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
26 changes: 15 additions & 11 deletions polars/polars-core/src/chunked_array/ops/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,23 +813,27 @@ impl ChunkAggSeries for BooleanChunked {
}
}

macro_rules! one_null_utf8 {
($self:ident) => {{
let mut builder = Utf8ChunkedBuilder::new($self.name(), 1, 0);
builder.append_null();
builder.finish().into_series()
}};
}

impl ChunkAggSeries for Utf8Chunked {
fn sum_as_series(&self) -> Series {
one_null_utf8!(self)
Utf8Chunked::full_null(self.name(), 1).into_series()
}
fn max_as_series(&self) -> Series {
one_null_utf8!(self)
Series::new(
self.name(),
&[self
.downcast_iter()
.filter_map(compute::aggregate::max_string)
.fold_first_(|acc, v| if acc > v { acc } else { v })],
)
}
fn min_as_series(&self) -> Series {
one_null_utf8!(self)
Series::new(
self.name(),
&[self
.downcast_iter()
.filter_map(compute::aggregate::min_string)
.fold_first_(|acc, v| if acc < v { acc } else { v })],
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions py-polars/polars/internals/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ def product(self) -> int | float:
"""Reduce this Series to the product value."""
return self.to_frame().select(pli.col(self.name).product()).to_series()[0]

def min(self) -> int | float | date | datetime | timedelta:
def min(self) -> int | float | date | datetime | timedelta | str:
"""
Get the minimal value in this Series.
Expand All @@ -924,7 +924,7 @@ def min(self) -> int | float | date | datetime | timedelta:
"""
return self._s.min()

def max(self) -> int | float | date | datetime | timedelta:
def max(self) -> int | float | date | datetime | timedelta | str:
"""
Get the maximum value in this Series.
Expand Down
7 changes: 7 additions & 0 deletions py-polars/tests/test_utf8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import polars as pl


def test_min_max_agg_on_str() -> None:
strings = ["b", "a", "x"]
s = pl.Series(strings)
assert (s.min(), s.max()) == ("a", "x")

0 comments on commit 6a0e78c

Please sign in to comment.