BUG: fix empty groupby(..., as_index=False).apply(...) index name inconsistency#65332
Open
Ashish-ml-eng wants to merge 2 commits intopandas-dev:mainfrom
Open
BUG: fix empty groupby(..., as_index=False).apply(...) index name inconsistency#65332Ashish-ml-eng wants to merge 2 commits intopandas-dev:mainfrom
Ashish-ml-eng wants to merge 2 commits intopandas-dev:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes an inconsistency in DataFrame.groupby(..., as_index=False).apply(...) where empty inputs previously produced an index with the group key name (e.g. ['A']), while non-empty inputs produced unnamed index levels ([None]).
Changes:
- Adjust
DataFrameGroupBy._wrap_applied_outputempty-values handling to setres_index=Nonewhenas_index=False. - Add a regression test asserting index name parity between empty and non-empty
groupby(..., as_index=False).apply(...).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pandas/core/groupby/generic.py |
Aligns empty-apply index construction with the non-empty as_index=False behavior by avoiding result_index in the empty path. |
pandas/tests/groupby/test_apply.py |
Adds coverage to ensure empty and non-empty results have consistent index.names when as_index=False. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
6 tasks
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.
Closes #48135
What happened
DataFrame.groupby(..., as_index=False).apply(...)behaved differently for empty vs non-empty input:result.index.names == [None]result.index.names == ['A'](group key name)What this PR changes
In the empty-values path of
DataFrameGroupBy._wrap_applied_output(
pandas/core/groupby/generic.py), this PR makesas_index=Falseuseres_index = Noneinstead ofself._grouper.result_index.This keeps empty-output behavior consistent with non-empty output when
as_index=False.Tests
Added a regression test in
pandas/tests/groupby/test_apply.pythat checks:[None]