-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: groupby.apply respects as_index=False if and only if group_keys=True #57656
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
Comments
import pandas as pd
df = pd.DataFrame({'A': [7, -1, 4, 5], 'B': [10, 4, 2, 8]}, index= pd.Index(['i3', 'i2', 'i1', 'i0'], name='i0'))
################################
# For transforms, like lambda x: x
################################
# when group_keys=True, apply() sorts if and only if sort=True as well.
print(df.groupby('A', sort=True, group_keys=True).apply(lambda x: x, include_groups=False))
print(df.groupby('A', sort=False, group_keys=True).apply(lambda x: x, include_groups=False))
# when group_keys=False, never sort.
print(df.groupby('A', sort=True, group_keys=False).apply(lambda x: x, include_groups=False))
print(df.groupby('A', sort=False, group_keys=False).apply(lambda x: x, include_groups=False))
################################
# For non-transform lambda x: pd.DataFrame([x.iloc[0].sum()])
################################
# when group_keys=True, apply() respects sort=True and sort=False.
print(df.groupby('A', sort=True, group_keys=True).apply(lambda x: pd.DataFrame([x.iloc[0].sum()]), include_groups=False))
print(df.groupby('A', sort=False, group_keys=True).apply(lambda x: pd.DataFrame([x.iloc[0].sum()]), include_groups=False))
# when group_keys=False, apply() respects sort=True and sort=False.
print(df.groupby('A', sort=True, group_keys=False).apply(lambda x: pd.DataFrame([x.iloc[0].sum()]), include_groups=False))
print(df.groupby('A', sort=False, group_keys=False).apply(lambda x: pd.DataFrame([x.iloc[0].sum()]), include_groups=False)) edit: see below for comment about |
correction for Rather than following following the usual interpretation of either sort=True or sort=False, it seems that when import pandas as pd
df = pd.DataFrame({'A': [7, -1, 4, 7], 'B': [10, 4, 2, 8]}, index= pd.Index(['i3', 'i2', 'i1', 'i0'], name='i0'))
################################
# For transforms, like lambda x: x
################################
# when group_keys=True, sort means the usual thing: sort = True means sort by values of group keys. sort = False
# means sort by order of appearance of group keys.
print(df.groupby('A', sort=True, group_keys=True).apply(lambda x: x, include_groups=False))
print(df.groupby('A', sort=False, group_keys=True).apply(lambda x: x, include_groups=False))
# when group_keys=False, reindex result to the index of the original dataframe. sort param has no effect.
print(df.groupby('A', sort=True, group_keys=False).apply(lambda x: x, include_groups=False))
print(df.groupby('A', sort=False, group_keys=False).apply(lambda x: x, include_groups=False)) |
@mvashishtha - would you be able to condense this back into the OP? |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
groupby.apply respects as_index=False if and only if
group_keys=True
, but the documentation suggests that it should only respectas_index
ifgroup_keys=False
.My apologies in advance if I'm duplicating an issue or misunderstanding the intended behavior here. I know there has been some relevant discussion in #49543.
Expected Behavior
I don't know what the correct behavior is here. A simple and easily explainable behavior would be to always respect
as_index=False
. However, to be consistent with the documentation here, transform-like applies should never respectas_index=False
, and I suppose that non-transform-like applies should respect it:When
group_keys=True
, the result does include the "groupings that are used to split the result", so for the same reason that this note gives,as_index
should have no effect. The current behavior is the opposite, though:as_index
has an effect only whengroup_keys=True
. (despite the description of group_keys, it appears thatapply
includes the group keys in the index if and only ifgroup_keys=False
, regardless of whetherfunc
is a transform.)Installed Versions
The text was updated successfully, but these errors were encountered: