DEPR: groupby with as_index=False doesn't add grouper as column when passing a Series as group key · Issue #49519 · pandas-dev/pandas (original) (raw)

I don't know if this is strictly speaking a but (the documentation doesn't really specify it), but I would have expected a different result:

In [28]: df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

In [29]: key = pd.Series(['foo', 'foo', 'bar'], name="key")

In [30]: df.groupby(key).sum()
Out[30]: 
     a  b
key      
bar  3  6
foo  3  9

In [31]: df.groupby(key, as_index=False).sum()
Out[31]: 
   a  b
0  3  6
1  3  9

(running with current main, 2.0.0.dev0+532.gdec9be2a5c)

When your key is a column in the DataFrame, specifying as_index=False will move the group key values to a column in the resulting dataframe:

In [34]: df["key"] = key

In [35]: df.groupby("key", as_index=False).sum()
Out[35]: 
   key  a  b
0  bar  3  6
1  foo  3  9

I would have expected the same behaviour if you pass the key as a Series.

cc @rhshadrach

This is related to #49450 (comment), where I encountered this behaviour for the case where the Series is actually a column of the original DataFrame (when changing this Series to be a view, and not an identical object).