Skip to content

BUG: keep groupby apply return type consistent - #65709

Closed
Will-thom wants to merge 3 commits into
pandas-dev:mainfrom
Will-thom:fix-groupby-apply-type-consistency
Closed

BUG: keep groupby apply return type consistent#65709
Will-thom wants to merge 3 commits into
pandas-dev:mainfrom
Will-thom:fix-groupby-apply-type-consistency

Conversation

@Will-thom

@Will-thom Will-thom commented May 22, 2026

Copy link
Copy Markdown

Tests:

  • docker exec pandas-gh54992-test bash -lc "cd /workspace && python -m pytest pandas/tests/groupby/test_apply.py::test_apply_single_group_returns_series_like_multiple_groups -q"
  • docker exec pandas-gh54992-test bash -lc "cd /workspace && python -m pytest pandas/tests/groupby/test_apply.py -q"
  • docker exec pandas-gh54992-test bash -lc "cd /workspace && python -m pytest pandas/tests/groupby -q"

@Will-thom
Will-thom requested a review from rhshadrach as a code owner May 22, 2026 13:41
@rhshadrach

Copy link
Copy Markdown
Member

Thanks for the PR! If I don't get a review in by June 1st, please ping me.

@rhshadrach rhshadrach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, but I'm leaning negative on introducing more behaviors such as the one being done here. The conditions on the result index equaling / not equaling the index / columns of the input is fragile and quite surprising. E.g. just changing a 2 to 3 below completely changes the result shape.

def foo(x):
    return pd.Series({0: x['b'].sum(), 1: x['b'].mean(), 2: x['b'].std()})

df = pd.DataFrame({'a': [1, 1, 1], 'b': [3, 4, 7]})
print(df.groupby('a').apply(foo))
# a   
# 1  0    14.000000
#    1     4.666667
#    2     2.081666
# dtype: float64

def foo(x):
    return pd.Series({0: x['b'].sum(), 1: x['b'].mean(), 3: x['b'].std()})

df = pd.DataFrame({'a': [1, 1, 1], 'b': [3, 4, 7]})
print(df.groupby('a').apply(foo))
#       0         1         3
# a                          
# 1  14.0  4.666667  2.081666

Now I think one could argue that this case is likely to be uncommon, which is what is keeping me from being strongly opposed to this kind of change, but I still do not think this is the right kind of solution we want.

@Will-thom

Copy link
Copy Markdown
Author

Hi @rhshadrach, gently pinging this as suggested since June 1 has passed. Please let me know if there is anything else needed from my side. Thanks!

@rhshadrach

Copy link
Copy Markdown
Member

@Will-thom - I left my feedback above.

@jbrockmendel

Copy link
Copy Markdown
Member

closing as stale. can reopen when you're ready to address comments and continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: pandas groupby.apply return type changes depending on number of unique groupkeys for same custom-function

3 participants