Skip to content

BUG: misleading error message when aggregating duplicate column names in groupby #44924

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
batterseapower opened this issue Dec 16, 2021 · 1 comment · Fixed by #44944
Closed
2 of 3 tasks
Labels
Bug Error Reporting Incorrect or improved errors from pandas Groupby Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@batterseapower
Copy link
Contributor

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

pd.DataFrame.from_dict({
    'A': [1, 2],
    'B': [3, 3],
    'C': ['G', 'G'],
}).groupby('C')[['A', 'B', 'A']].mean()

Issue Description

The above fails with an error like this:

  File "C:\Users\mboling\Anaconda3\envs\pandastest2\lib\site-packages\pandas\core\groupby\generic.py", line 1538, in __getitem__
    return super().__getitem__(key)
  File "C:\Users\mboling\Anaconda3\envs\pandastest2\lib\site-packages\pandas\core\base.py", line 222, in __getitem__
    raise KeyError(f"Columns not found: {str(bad_keys)[1:-1]}")
KeyError: 'Columns not found: '

This is misleading -- the A and B columns are all present in the input, the real problem is that the A column is repeated.

Expected Behavior

It would be better to either not fail in this case (i.e. return a DataFrame with duplicate column names) or fail with an error message that mentions that the input columns contain duplicates.

Installed Versions

Tested on Pandas 1.3.4, but that install has a broken show_versions for some reason, so this is show_versions from an older install that also exhibits the problem.

INSTALLED VERSIONS ------------------ commit : 945c9ed python : 3.8.12.final.0 python-bits : 64 OS : Windows OS-release : 10 Version : 10.0.18363 machine : AMD64 processor : Intel64 Family 6 Model 85 Stepping 7, GenuineIntel byteorder : little LC_ALL : None LANG : None LOCALE : English_United Kingdom.1252

pandas : 1.3.4
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 21.1.1
setuptools : 58.0.4
Cython : 0.29.24
pytest : 6.2.4
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.29.0
pandas_datareader: None
bs4 : 4.10.0
bottleneck : 1.3.2
fsspec : 2021.08.1
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : 0.19.0
xlrd : 2.0.1
xlwt : None
numba : 0.53.1

@batterseapower batterseapower added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 16, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Dec 17, 2021
@simonjayhawkins
Copy link
Member

It would be better to either not fail in this case (i.e. return a DataFrame with duplicate column names) or

This was the behavior before 1.1.5

first bad commit: [e99e5ab] BUG: Fix duplicates in intersection of multiindexes (#36927)

cc @phofl

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version Groupby and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 17, 2021
@jreback jreback added this to the 1.4 milestone Dec 17, 2021
@jreback jreback added the Error Reporting Incorrect or improved errors from pandas label Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Error Reporting Incorrect or improved errors from pandas 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.

3 participants