Skip to content

[QST] Clarify when agg() will return groups as index #32579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
shwina opened this issue Mar 10, 2020 · 0 comments · Fixed by #34012
Closed

[QST] Clarify when agg() will return groups as index #32579

shwina opened this issue Mar 10, 2020 · 0 comments · Fixed by #34012
Labels
API - Consistency Internal Consistency of API/Behavior Groupby
Milestone

Comments

@shwina
Copy link
Contributor

shwina commented Mar 10, 2020

In [44]: a = pd.DataFrame({'a': [1, 1, 2], 'b': [1, 2, 3]})

In [45]: a.groupby('a').agg('sum') # 'a' is returned as index
Out[45]:
   b
a
1  3
2  3

In [46]: a.groupby('a').agg('nunique') # 'a' is returned both as index and column
Out[46]:
   a  b
a
1  1  2
2  1  1

The groupby documentation notes:

Aggregation functions will not return the groups that you are aggregating over if they are named columns, when as_index=True, the default. The grouped columns will be the indices of the returned object.

However, in the snippet above, it looks like the nunique aggregation behaves differently in this respect from the sum aggregation.

Is it possible to predict when an aggregation will return groups as index columns v/s a "SQL-style output" v/s a combination of the two (as above)?

@jreback jreback added API - Consistency Internal Consistency of API/Behavior Groupby labels May 14, 2020
@jreback jreback added this to the 1.1 milestone May 14, 2020
TomAugspurger added a commit to TomAugspurger/dask that referenced this issue Jun 16, 2020
* idxmaxmin: pandas-dev/pandas#32579
* upstream fix
* first_last: pandas-dev/pandas#34167
* test_dropna: unclear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants