Skip to content

Conversation

snitish
Copy link
Member

@snitish snitish commented Dec 6, 2024

…mes/series

When using groupby.apply on an empty dataframe or series with group_keys=False, the output index is incorrectly set to the grouping keys, while it is expected to be a RangeIndex(0, 0, 1).
From the original issue:

>>> df = pd.DataFrame({'A': [], 'B': [], 'C': []})
>>> g2 = df.groupby('A', group_keys=False)
>>> r2 = g2.apply(lambda x: x / x.sum())
>>> print(r2.index)
Index([], dtype='float64', name='A')

Expected:

>>> print(r2.index)
RangeIndex(start=0, stop=0, step=1)

@snitish snitish requested a review from rhshadrach as a code owner December 6, 2024 08:07
@mroeschke mroeschke added Apply Apply, Aggregate, Transform, Map Groupby labels Dec 6, 2024
@mroeschke mroeschke added this to the 3.0 milestone Dec 6, 2024
@mroeschke mroeschke merged commit 8a286fa into pandas-dev:main Dec 6, 2024
59 checks passed
@mroeschke
Copy link
Member

Thanks @snitish

@snitish snitish deleted the issue60471 branch February 6, 2025 19:46
KevsterAmp pushed a commit to KevsterAmp/pandas that referenced this pull request Mar 12, 2025
pandas-dev#60505)

BUG: Fix bug in GroupBy that ignores group_keys arg for empty dataframes/series
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apply Apply, Aggregate, Transform, Map Groupby

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: DataFrameGroupBy.apply ignores group_keys setting when empty

2 participants