Skip to content
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

BUG: AttributeError when doing groupby with as_index=False on Empty DataFrame #35246

Closed
3 tasks done
salem3358 opened this issue Jul 12, 2020 · 9 comments · Fixed by #35324
Closed
3 tasks done

BUG: AttributeError when doing groupby with as_index=False on Empty DataFrame #35246

salem3358 opened this issue Jul 12, 2020 · 9 comments · Fixed by #35324
Assignees
Labels
Bug Groupby Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@salem3358
Copy link
Contributor

salem3358 commented Jul 12, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

# Your code here
import pandas as pd
pd.DataFrame(columns=["a", "b"]).groupby(by="a", as_index=False)["b"].sum()

Problem description

in version 0.25.3, this code return an empty DataFrame with columns=["a", "b"]

Expected Output

an empty DataFrame with columns=["a", "b"]

Output of pd.show_versions()

[paste the output of pd.show_versions() here leaving a blank line after the details tag]

INSTALLED VERSIONS

commit : None
python : 3.6.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 85 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.5
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 19.0.3
setuptools : 40.8.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 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@salem3358 salem3358 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 12, 2020
@salem3358 salem3358 changed the title BUG: AttributeError when doing groupby with index BUG: AttributeError when doing groupby with as_index=False Jul 12, 2020
@salem3358 salem3358 changed the title BUG: AttributeError when doing groupby with as_index=False BUG: AttributeError when doing groupby with as_index=False on Empty DataFrame Jul 12, 2020
@neil-ptr
Copy link

I'd be interested in looking into this

@jorisvandenbossche jorisvandenbossche added Groupby Regression Functionality that used to work in a prior pandas version and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 12, 2020
@jorisvandenbossche
Copy link
Member

@neilZon that's certainly welcome! (and @salem3358 thanks for the report)

@simonjayhawkins
Copy link
Member

simonjayhawkins commented Jul 12, 2020

the exception here is being raised by code added in #32040 (fix for #31777) i.e. 1.0.2

however, before this change, the example was still failing so the underlying issue maybe elsewhere

1.1.0.dev0+776.g9e7cb7c10
Traceback (most recent call last):
  File "C:/Users/simon/test.py", line 7, in <module>
    res = pd.DataFrame(columns=["a", "b"]).groupby(by="a", as_index=False)["b"].sum()
  File "c:\users\simon\pandas\pandas\core\groupby\groupby.py", line 1398, in func
    result = self.aggregate(lambda x: npfunc(x, axis=self.axis))
  File "c:\users\simon\pandas\pandas\core\groupby\generic.py", line 959, in aggregate
    result.columns.levels[0], name=self._selected_obj.columns.name
AttributeError: 'Index' object has no attribute 'levels'

cc @MarcoGorelli

@simonjayhawkins
Copy link
Member

however, before this change, the example was still failing so the underlying issue maybe elsewhere

#28203 is the cause of the regression from 0.25.3

bfdbebe is the first bad commit
commit bfdbebe
Author: jbrockmendel [email protected]
Date: Fri Aug 30 11:43:48 2019 -0700

CLN: catch less inside try/except (#28203)

cc @jbrockmendel

@salem3358
Copy link
Contributor Author

salem3358 commented Jul 15, 2020

Hi guys,
Do we have any update on this issue?
Can I push a Pull request to fix this? I mean what is the procedure? I guess I will need to register to become a contributor first, right?

Thanks,
Salem

@MarcoGorelli
Copy link
Member

@salem3358 Sure, here's the contributing guide: https://pandas.pydata.org/pandas-docs/dev/development/contributing.html

If you comment take the issue will be assigned to you. @neilZon are you still working on this? If not, then @salem3358 free to take over

@neil-ptr
Copy link

Nope, @salem3358 is free to take!

@salem3358
Copy link
Contributor Author

take

@salem3358
Copy link
Contributor Author

#35324

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants