Skip to content

BUG: Inconsistency when empty inputs are passed to agg for groupby #48581

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

Open
3 tasks done
ntachukwu opened this issue Sep 16, 2022 · 3 comments
Open
3 tasks done

BUG: Inconsistency when empty inputs are passed to agg for groupby #48581

ntachukwu opened this issue Sep 16, 2022 · 3 comments
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby

Comments

@ntachukwu
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 main branch of pandas.

Reproducible Example

>>> import pandas as pd

>>> df = pd.DataFrame(data={'a': [1, 2, 3], 'b': [4, 5, 6]})
>>> print(df.groupby("a").agg([])) # Passing an empty list as argument
Empty DataFrame
Columns: []
Index: []
>>> print(df.groupby("a").agg(())) # Passing an empty tuple as argument
Empty DataFrame
Columns: []
Index: []
>>> print(df.groupby("a").agg({})) # Passing an empty dictionary as argument
....
ValueError: No objects to concatenate

Issue Description

Passing an empty list or tuple to groupby.agg returns an empty dataframe but an empty dictionary raises a ValueError.

This is part of an effort to find consistency between pandas operations with empty inputs. Issue #47959 is an attempt to start a discussion on this.

Expected Behavior

Some Consistency

Installed Versions

INSTALLED VERSIONS ------------------ commit : 519fa10 python : 3.9.10.final.0 python-bits : 64 OS : Darwin OS-release : 21.3.0 Version : Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_ARM64_T8101 machine : arm64 processor : arm byteorder : little LC_ALL : None LANG : None LOCALE : None.UTF-8

pandas : 1.5.0.dev0+1478.g94044c8532
numpy : 1.22.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 60.9.3
pip : 22.1.1
Cython : 0.29.32
pytest : 7.1.2
hypothesis : 6.52.3
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.3.0
pandas_datareader: 0.10.0
bs4 : 4.11.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@ntachukwu ntachukwu added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 16, 2022
@eshanja1n
Copy link

take

@topper-123
Copy link
Contributor

Thanks for this report, @Th3nn3ss.

I agree that these should behave similarly for consistency. I'm not super sure however ig I think that these should work or whether we should instead raise if given an empty list/dict. @rhshadrach, do you have an opinion?

If we do accept empty lists/dicts IMO it would be most logical to still keep the groupings, in the examples above return

Empty DataFrame
Columns: []
Index: [1, 2, 3]

That is, always return the same index, even if called with empty lists/dicts.

@topper-123 topper-123 added Groupby API - Consistency Internal Consistency of API/Behavior and removed API - Consistency Internal Consistency of API/Behavior Needs Triage Issue that has not been reviewed by a pandas team member labels May 20, 2023
@rhshadrach
Copy link
Member

+1 on not raising here, and returning the frame @topper-123 suggested.

@rhshadrach rhshadrach added the Apply Apply, Aggregate, Transform, Map label May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform, Map Bug Groupby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants