Skip to content

Commit

Permalink
fix so rank (#4114)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 22, 2022
1 parent 480dd14 commit dd0cbbc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/unique/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn rank(s: &Series, method: RankMethod, reverse: bool) -> Series {

// Currently, nulls tie with the minimum or maximum bound for a type, depending on reverse.
// TODO: Need to expose nulls_last in argsort to prevent this.
if s.has_validity() {
if s.null_count() > 0 {
// Fill using MaxBound/MinBound to keep nulls first.
let null_strategy = if reverse {
FillNullStrategy::MaxBound
Expand Down
19 changes: 19 additions & 0 deletions py-polars/tests/test_exprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,22 @@ def test_arr_contains() -> None:
).collect().to_dict(False) == {
"str_list": [["cat", "mouse", "dog"], ["dog", "mouse", "cat"]]
}


def test_rank_so_4109() -> None:
df = pl.from_dict(
{
"id": [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4],
"rank": [None, 3, 2, 4, 1, 4, 3, 2, 1, None, 3, 4, 4, 1, None, 3],
}
).sort(by=["id", "rank"])

assert df.groupby("id").agg(pl.col("rank").rank()).to_dict(False) == {
"id": [1, 2, 3, 4],
"rank": [
[1.0, 2.0, 3.0, 4.0],
[1.0, 2.0, 3.0, 4.0],
[1.0, 2.0, 3.0, 4.0],
[1.0, 2.0, 3.0, 4.0],
],
}

0 comments on commit dd0cbbc

Please sign in to comment.