Skip to content

BUG: DataFrame.agg() on empty dataframe returns unexpected result #60424

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
kdebrab opened this issue Nov 26, 2024 · 3 comments
Closed
2 of 3 tasks

BUG: DataFrame.agg() on empty dataframe returns unexpected result #60424

kdebrab opened this issue Nov 26, 2024 · 3 comments
Labels
Apply Apply, Aggregate, Transform, Map Bug

Comments

@kdebrab
Copy link
Contributor

kdebrab commented Nov 26, 2024

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

index_empty = pd.MultiIndex.from_product([[], []], names=["one", "two"])
df_empty = pd.DataFrame({"a": [], "b": []}, index=index_empty)
df_empty.agg(lambda row: row.name[0], axis=1)

Issue Description

The above returns:

Empty DataFrame
Columns: [a, b]
Index: []

Expected Behavior

The expected result would be an empty series:

Series([], dtype: float64)

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.11.3
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 140 Stepping 1, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Belgium.1252
pandas : 2.2.3
numpy : 2.1.3
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : 8.28.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : 1.4.2
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.10.0
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : 5.3.0
matplotlib : 3.8.4
numba : None
numexpr : 2.10.2
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : 1.4.6
pyarrow : 18.0.0
pyreadstat : None
pytest : 8.3.3
python-calamine : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.14.1
sqlalchemy : 2.0.36
tables : 3.10.1
tabulate : 0.9.0
xarray : 2024.10.0
xlrd : None
xlsxwriter : 3.2.0
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

@kdebrab kdebrab added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 26, 2024
@rhshadrach
Copy link
Member

Thanks for the report. Closing as a duplicate of #47959.

@rhshadrach rhshadrach added Apply Apply, Aggregate, Transform, Map and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 26, 2024
@kdebrab
Copy link
Contributor Author

kdebrab commented Nov 27, 2024

@rhshadrach Thanks for linking with #47959.

I would like to add a bit more context.

Note that e.g. following does work as expected on an empty dataframe:

df_empty.agg(lambda row: row.name, axis=1)
Out[3]: Series([], dtype: float64)

It's only when adding a subscript to row.name (which should be fine as the index is a multiindex), we suddenly get:

df_empty.agg(lambda row: row.name[0], axis=1)
Out[4]: 
Empty DataFrame
Columns: [a, b]
Index: []

@rhshadrach
Copy link
Member

@kdebrab - this is because agg will attempt to pass an empty Series, using the result to determine what it should return. By adding [0], this attempt now raises an error, meaning that agg has no information whatsoever on what the UDF you're providing does. As such, it just returns the input as-is.

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
Projects
None yet
Development

No branches or pull requests

2 participants