Skip to content

Commit

Permalink
fix(rust, python): fix all_null/sorted into_groups panic (#5733)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Dec 7, 2022
1 parent 3621a50 commit c01032a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions polars/polars-core/src/frame/groupby/into_groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ where
}
let mut values = arr.values().as_slice();
let null_count = arr.null_count();
let length = values.len();

// all nulls
if null_count == length {
return vec![[0, length as IdxSize]];
}

let mut nulls_first = false;
if null_count > 0 {
Expand All @@ -92,8 +98,8 @@ where
if nulls_first {
values = &values[null_count..];
} else {
values = &values[..values.len() - null_count];
}
values = &values[..length - null_count];
};

let n_threads = POOL.current_num_threads();
let groups = if multithreaded && n_threads > 1 {
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,17 @@ def test_groupby_wildcard() -> None:
assert df.groupby([pl.col("*")], maintain_order=True).agg(
[pl.col("a").first().suffix("_agg")]
).to_dict(False) == {"a": [1, 2], "b": [1, 2], "a_agg": [1, 2]}


def test_groupby_all_masked_out() -> None:
df = pl.DataFrame(
{
"val": pl.Series(
[None, None, None, None], dtype=pl.Categorical, nan_to_null=True
).set_sorted(),
"col": [4, 4, 4, 4],
}
)
parts = df.partition_by("val")
assert len(parts) == 1
assert parts[0].frame_equal(df)

0 comments on commit c01032a

Please sign in to comment.