diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 722de91ba5246..a9a110af857d1 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -413,11 +413,19 @@ def agg_dict_like(self) -> DataFrame | Series: keys_to_use = ktu axis: AxisInt = 0 if isinstance(obj, ABCSeries) else 1 - result = concat( - {k: results[k] for k in keys_to_use}, - axis=axis, - keys=keys_to_use, - ) + + # GH 48581 - Adding consistency to empty agg types + if not results: + # return empty DataFrame if results is empty, following pattern + # empty list and tuple + from pandas import DataFrame + + result = DataFrame() + else: + result = concat( + {k: results[k] for k in keys_to_use}, axis=axis, keys=keys_to_use + ) + elif any(is_ndframe): # There is a mix of NDFrames and scalars raise ValueError( diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 03b917edd357b..2d4c50d693133 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1443,3 +1443,12 @@ def test_agg_of_mode_list(test, constant): expected = expected.set_index(0) tm.assert_frame_equal(result, expected) + + +def test_empty_container_consistency(): + df = DataFrame(data={"a": [1, 2], "b": [3, 4], "c": [5, 6]}) + dict_result = df.groupby("a").agg({}) + + expected = DataFrame() + + tm.assert_frame_equal(dict_result, expected)