Support skipna in groupby reductions (first/last, sum/prod/mean/median/min/max, idxmin/idxmax) - #22925
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
skipna in groupby first and lastskipna in groupby reductions (first/last, sum/prod/mean/median/min/max, idxmin/idxmax)
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
💤 Files with no reviewable changes (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughAdds ChangesGroupBy skipna support
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested labels
Suggested reviewers
Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@python/cudf/cudf/core/groupby/groupby.py`:
- Around line 1454-1465: The code at line 1459 compares non_null_counts[name]
Series values against group_sizes, but when as_index=False, self.size() returns
a DataFrame instead of a Series, causing a shape/type mismatch in the
comparison. Extract the actual group size values from the group_sizes DataFrame
when it is a DataFrame (which occurs when as_index=False) before using it in the
comparison operation within the all_non_null DataFrame comprehension. Ensure the
extracted values can be properly broadcast with the Series comparison for each
column name.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: cb2cd638-ba2d-4421-a67b-6d80149e14cb
📒 Files selected for processing (4)
python/cudf/cudf/core/_internals/aggregation.pypython/cudf/cudf/core/groupby/groupby.pypython/cudf/cudf/pandas/scripts/pandas-testing-plugin.pypython/cudf/cudf/tests/groupby/test_reductions.py
|
Thanks @mroeschke. Removed the option_context blocks from the new tests; the skipna logic in groupby.py runs regardless of pandas compatible mode, so they were not needed. For first/last I dropped the sort parametrization and pinned sort=True, since the only thing that needed compatible mode there was group output ordering, which is unrelated to skipna. I also merged the two reduction tests into one that reuses the as_index fixture from conftest.py. The remaining tests parametrize on first/last and skipna, which the existing conftest fixtures do not cover. |
|
/merge |
Description
GroupByreductions ignored theirskipnaargument: nulls were always dropped, regardless ofskipna. This PR aligns groupbyskipnahandling with pandas across the reductions:first/last—Aggregation.first/lasttakeskipna; withskipna=Falsethe actual first/last element of each group is returned even when it is null (previously the first/last non-null value was returned). Threaded through_reducevia a small_FirstLastAggSpeccallable agg-spec whose__str__/__name__report the op name so validity checks and result naming are unchanged.sum/prod/mean/median/min/max— withskipna=False, a group containing any null in a column now yields a null result for that (group, column), matching pandas (libcudf otherwise always drops nulls). Implemented in_reduceby masking the result where a column's non-null count is less than the group size (usingsize()rather than thesizeaggregation, which is unsupported for string columns).idxmin/idxmax— now raiseValueError("idxmin/idxmax with skipna=False"), matching pandas, which cannot represent the label of a NA (previously cudf returned an incorrect positional result).This drops the now-passing
skipnaxfail entries in thecudf.pandaspandas-test plugin fortests/groupby/test_reductions.py(test_first_last_skipna,test_mean_skipna,test_sum_skipna,test_multifunc_skipna,test_idxmin_idxmax_extremes_skipna).Intentionally still xfailed (not
skipna=Falsebugs)test_sum_skipna_object[False]— inherent cudf.pandas None-vs-NaN difference for object-dtype nulls (the skipna logic is correct; only null representation differs).test_multifunc_skipna[True-prod-values3]— an all-nullprodshould return the empty-product identity1.0; cudf returns NA. This is min_count/empty-reduction semantics (skipna=True), not theskipna=Falsebehavior fixed here.var/stddo not yet acceptskipna(their explicit methods omit it and fall back to pandas undercudf.pandas), so they are out of scope here.Tests
Added cuDF unit tests in
tests/groupby/test_reductions.py:test_groupby_first_last_skipna/test_groupby_series_first_last_skipnatest_groupby_reduction_skipna_false(sum/prod/mean/median/min/max × nullable + numpy dtypes)test_groupby_idxmin_idxmax_skipna_false_raises(DataFrame and Series groupby)Each fails without the corresponding fix.
Verification
pandas-testing/.../test_reductions.pyundercudf.pandas(with plugin): 1311 passed, 53 xfailed, 0 failed, 0 XPASS.Checklist