Skip to content
15 changes: 15 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1651,3 +1651,18 @@ def test_groupby_agg_extension_timedelta_cumsum_with_named_aggregation():
gb = df.groupby("grps")
result = gb.agg(td=("td", "cumsum"))
tm.assert_frame_equal(result, expected)


def test_groupby_aggregation_empty_group():
# https://github.com/pandas-dev/pandas/issues/18869
def func(x):
if len(x) == 0:
raise ValueError("length must not be 0")
return len(x)

df = DataFrame(
{"A": pd.Categorical(["a", "a"], categories=["a", "b", "c"]), "B": [1, 1]}
)
msg = "length must not be 0"
with pytest.raises(ValueError, match=msg):
df.groupby("A", observed=False).agg(func)