Skip to content

BUG: Dataframe.aggregate has inconsistent behaviour for empty dataframe #62443

@flori-ko

Description

@flori-ko

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({"A": ["A"], "B": ["B"]})
# This works
df["C"] = df.aggregate(".".join, axis="columns")

empty_df = pd.DataFrame({"A": [], "B": []})
# This raises an error as the return value is a dataframe not a series
empty_df["C"] = empty_df.aggregate(".".join, axis="columns")

Issue Description

When using df.aggregate the return value is inconsistent/ incorrect for an empty dataframe. In this case, the same empty dataframe is returned instead of a single aggregated column, which fails if the result is supposed to be applied to a single column.

Expected Behavior

I would expect df.aggregate to return an empty Series for an empty dataframe as it would be more consistent and easier to use. Also the documentation of df.aggregate states:

Returns:
scalar, Series or DataFrame
The return can be:
scalar : when Series.agg is called with single function
Series : when DataFrame.agg is called with a single function
DataFrame : when DataFrame.agg is called with several functions

Installed Versions

INSTALLED VERSIONS

commit : 4665c10
python : 3.11.9
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 143 Stepping 8, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : English_United States.1252

pandas : 2.3.2
numpy : 2.3.3
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.2
Cython : None
sphinx : 8.2.3
IPython : 9.5.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.5
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : 3.1.6
lxml.etree : 6.0.2
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : 20.0.0
pyreadstat : None
pytest : 8.4.2
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.15.3
sqlalchemy : 2.0.43
tables : None
tabulate : 0.9.0
xarray : 2025.9.0
xlrd : None
xlsxwriter : 3.2.9
zstandard : None
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

ApplyApply, Aggregate, Transform, MapBug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions