Fix groupby().parallel_apply() passing grouping columns on pandas >= 3.0 - #282
Open
LalwaniPalash wants to merge 1 commit into
Open
Fix groupby().parallel_apply() passing grouping columns on pandas >= 3.0#282LalwaniPalash wants to merge 1 commit into
groupby().parallel_apply() passing grouping columns on pandas >= 3.0#282LalwaniPalash wants to merge 1 commit into
Conversation
…das >= 3.0 Since pandas 3.0, DataFrameGroupBy.apply operates on _obj_with_exclusions, so the grouping columns are no longer passed to the applied function. Iterating a DataFrameGroupBy still yields them, which is how pandarallel builds its chunks, so parallel_apply kept passing them. This produced a silent divergence rather than an error: user functions that iterate columns or aggregate across the frame got different results from parallel_apply than from apply. Restrict each group to the columns apply would have used, gated on the pandas version so behaviour on pandas < 3.0 is unchanged. The groupby test fixture read df.b, but the test also covers groupby(["a", "b"]) where b is now a grouping column, making the reference apply() call raise on pandas 3.0. Switched the fixture to column c, which is never a grouping column in these tests.
There was a problem hiding this comment.
Pull request overview
This pull request aligns DataFrameGroupBy.parallel_apply() behavior with pandas ≥ 3.0’s DataFrameGroupBy.apply() by ensuring grouping columns are not included in the DataFrame passed to the user function (matching pandas’ _obj_with_exclusions behavior). It also updates the affected test fixture to avoid referencing a column that becomes a grouping column in one of the groupby test cases under pandas 3.0.
Changes:
- Update
DataFrameGroupBy.Apply.get_chunks()to slice each yielded group todataframe_groupby._obj_with_exclusions.columnswhen pandas ≥ 3.0. - Adjust
func_dataframe_groupby_applytest fixture to operate on columnc(notb) so referenceapplydoesn’t fail in thegroupby(["a", "b"])test case under pandas 3.0.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
pandarallel/data_types/dataframe_groupby.py |
Ensures group chunks match pandas ≥ 3.0 apply() semantics by excluding grouping columns before invoking the user function. |
tests/test_pandarallel.py |
Updates the groupby-apply fixture to avoid depending on a column that can become excluded as a grouping column in pandas ≥ 3.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since pandas 3.0,
DataFrameGroupBy.applyoperates on_obj_with_exclusions— the groupingcolumns are no longer passed to the applied function. Iterating a
DataFrameGroupBystillyields them, and that is how
pandarallelbuilds its chunks, soparallel_applystill passesthem.
The result is a silent divergence — no exception, just a different answer:
Any user function that iterates columns, calls
.sum()/.mean()across the frame, or buildsa result from
df.shapegets a different answer fromparallel_applythan fromapply.Fix
Restrict each group to the columns
applywould have used, gated on the pandas version sobehaviour on pandas < 3.0 is unchanged. Uses the existing
get_pandas_version()helper, matchingthe version-gating already used in
reduce()in this file.Tests
func_dataframe_groupby_applyreaddf.b, buttest_dataframe_groupby_applyalso coversgroupby(["a", "b"])— wherebis now a grouping column. On pandas 3.0 that fixture raisesAttributeErrorin the referenceapplycall, beforeparallel_applyis reached. Switchedthe fixture to column
c, which is never a grouping column in these tests.Verification
pandas 3.0.3, Python 3.14.6:
-k groupbyThe 16 remaining failures are all
test_dataframe_applymap—DataFrame.applymapwas removedin pandas 3.0. That is #280's scope, not this PR's. With #280 applied on top of this branch the
full suite is 217 passed, 0 failed.
Note on CI
CI is currently red on this repo independently of this change — the matrix targets
ubuntu-20.04(retired by GitHub, jobs are cancelled before running) and Python 3.7.9–3.10.8. The most recent
run, on #280, failed for this reason. The matrix also tops out at pandas 2.0, so it would not
have caught this bug and will not exercise this fix.
Because the change is gated on
get_pandas_version() >= (3, 0), it is a no-op on everycombination the matrix currently pins — so it cannot regress them.
Happy to send a separate PR refreshing the CI matrix (current runners, supported Python versions,
and a pandas 3.x entry) if that would be useful.