Skip to content

BUG: DataFrame.groupby silently ignores as_index=False when aggregating with multiple functions on a single column #45248

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
2 of 3 tasks
Demetrio92 opened this issue Jan 7, 2022 · 1 comment

Comments

@Demetrio92
Copy link

Demetrio92 commented Jan 7, 2022

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 master branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({
    'key': ['10', '10', '20', '20'],
    'value': [1, 2, 1, 1.5]
})

res1 = df.groupby('key', as_index=False)['value'].agg(['min', 'max'])
print('key' in res1.columns)   # False (!)
print(res1)
     min  max
key          
10   1.0  2.0
20   1.0  1.5

Issue Description

After using as_index=False in df.groupby() I was very surprised not to find the grouping column in the columns list. In fact, this option is completely ignored. In the example above as_index=True produces the same result.

Expected Behavior

As a workaround just adding .reset_index() at the end works

res2 = df.groupby('key')['value'].agg(['min', 'max']).reset_index()
print('key' in res2.columns)
print(res2)
  key  min  max
0  10  1.0  2.0
1  20  1.0  1.5

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.11.0-43-generic
Version : #47~20.04.2-Ubuntu SMP Mon Dec 13 11:06:56 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.3.4
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.3.1
setuptools : 58.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.4.25
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
numba : None

@Demetrio92 Demetrio92 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 7, 2022
@mroeschke
Copy link
Member

Thanks for the report but this is the expected behavior.

The docs for as_index states: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html

For aggregated output, return object with group labels as the index. Only relevant for DataFrame input

df.groupby('key', as_index=False)['value'] selects the value column from the DataFrame by turning input into a Series; therefore, as_index is a no-op here. Closing as a usage question

@mroeschke mroeschke added Groupby Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants