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

Created on 12 Jul 2020  路  9Comments  路  Source: pandas-dev/pandas

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

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

  • [x] (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

Bug Groupby Regression

All 9 comments

I'd be interested in looking into this

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

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

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

bfdbebec423d781ebde189de24f5413298ab7c81 is the first bad commit
commit bfdbebec423d781ebde189de24f5413298ab7c81
Author: jbrockmendel jbrockmendel@gmail.com
Date: Fri Aug 30 11:43:48 2019 -0700

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

cc @jbrockmendel

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

@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

Nope, @salem3358 is free to take!

take

35324

Was this page helpful?
0 / 5 - 0 ratings