Skip to content

Commit 165d80a

Browse files
BUG: DataFrame.aggregate returns Series for empty DataFrame with single function
1 parent 27fafee commit 165d80a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pandas/core/frame.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10501,14 +10501,24 @@ def _gotitem(
1050110501
)
1050210502
def aggregate(self, func=None, axis: Axis = 0, *args, **kwargs):
1050310503
from pandas.core.apply import frame_apply
10504-
10504+
from pandas import Series
1050510505
axis = self._get_axis_number(axis)
10506-
10506+
# Special case: empty DataFrame + single function
10507+
if self.empty and callable(func):
10508+
if axis == 1: # row-wise
10509+
return Series([], index=self.index, dtype=object)
10510+
else: # column-wise
10511+
# Column-wise on empty DataFrame -> empty Series
10512+
return Series([], dtype=object)
10513+
10514+
# Normal behavior
1050710515
op = frame_apply(self, func=func, axis=axis, args=args, kwargs=kwargs)
1050810516
result = op.agg()
1050910517
result = reconstruct_and_relabel_result(result, func, **kwargs)
1051010518
return result
1051110519

10520+
10521+
1051210522
agg = aggregate
1051310523

1051410524
@doc(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def test_aggregate_empty_dataframe_returns_series():
2+
import pandas as pd
3+
df = pd.DataFrame({"A": [], "B": []})
4+
# Row-wise aggregation (axis=1)
5+
result_row = df.aggregate(".".join, axis="columns")
6+
expected_row = pd.Series([], index=df.index, dtype=object)
7+
pd.testing.assert_series_equal(result_row, expected_row)
8+
# Column-wise aggregation (axis=0)
9+
result_col = df.aggregate(".".join, axis="index")
10+
expected_col = pd.Series([], dtype=object)
11+
pd.testing.assert_series_equal(result_col, expected_col)

0 commit comments

Comments
 (0)