Skip to content

Commit 432ffba

Browse files
committed
fix to maintain consistency for apply UDF on empty inputs
1 parent ac648ee commit 432ffba

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pandas/core/groupby/ops.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,18 +844,22 @@ def apply(
844844
if not mutated and not _is_indexed_like(res, group_axes, axis):
845845
mutated = True
846846
result_values.append(res)
847-
848847
# getattr pattern for __name__ is needed for functools.partial objects
849848
if len(group_keys) == 0 and getattr(f, "__name__", None) not in [
850849
"idxmin",
851850
"idxmax",
852851
"nanargmin",
853852
"nanargmax",
854853
]:
855-
# If group_keys is empty, then no function calls have been made,
856-
# so we will not have raised even if this is an invalid dtype.
857-
# So do one dummy call here to raise appropriate TypeError.
858-
f(data.iloc[:0])
854+
try:
855+
# If group_keys is empty, then no function calls have been made,
856+
# so we will not have raised even if this is an invalid dtype.
857+
# So do one dummy call here to raise appropriate TypeError.
858+
f(data.iloc[:0])
859+
except IndexError:
860+
# If IndexError is raised,
861+
# maintain consistency for all operations on empty groups
862+
pass
859863

860864
return result_values, mutated
861865

pandas/tests/groupby/test_apply.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,3 +1331,11 @@ def test_result_name_when_one_group(name):
13311331
expected = Series([1, 2], name=name)
13321332

13331333
tm.assert_series_equal(result, expected)
1334+
1335+
1336+
def test_empty_df():
1337+
empty_df = pd.DataFrame({"a": [], "b": []})
1338+
result = empty_df.groupby("a").b.apply(lambda x: x.values[-1])
1339+
expected = empty_df.groupby("a").b.take([0])
1340+
1341+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)