Skip to content

Commit

Permalink
fix stackoverflow in unique of empty series (#4389)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 13, 2022
1 parent 614b9dd commit fcbd488
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions polars/polars-core/src/chunked_array/ops/unique/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ where
ChunkedArray<T>: IntoSeries,
{
fn unique(&self) -> Result<Self> {
// prevent stackoverflow repeated sorted.unique call
if self.is_empty() {
return Ok(self.clone());
}
match self.is_sorted2() {
IsSorted::Ascending | IsSorted::Descending => {
let mask = self.not_equal(&self.shift(1));
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,9 @@ def test_rank_so_4109() -> None:
[1.0, 2.0, 3.0, 4.0],
],
}


def test_unique_empty() -> None:
for dt in [pl.Utf8, pl.Boolean, pl.Int32, pl.UInt32]:
s = pl.Series([], dtype=dt)
assert s.unique().series_equal(s)

0 comments on commit fcbd488

Please sign in to comment.