Skip to content

Commit be3efaa

Browse files
jorisvandenbosscheproost
authored andcommitted
REGR: fix DataFrame.agg case with list-like return value (pandas-dev#29632)
1 parent a4c5e98 commit be3efaa

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pandas/core/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,9 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
602602
if not len(results):
603603
raise ValueError("no results")
604604

605-
if all(np.ndim(x) > 0 for x in results):
605+
try:
606606
return concat(results, keys=keys, axis=1, sort=False)
607-
else:
607+
except TypeError:
608608

609609
# we are concatting non-NDFrame objects,
610610
# e.g. a list of scalars

pandas/tests/frame/test_apply.py

+17
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,23 @@ def test_non_callable_aggregates(self):
12591259

12601260
assert result == expected
12611261

1262+
def test_agg_listlike_result(self):
1263+
# GH-29587 user defined function returning list-likes
1264+
df = DataFrame(
1265+
{"A": [2, 2, 3], "B": [1.5, np.nan, 1.5], "C": ["foo", None, "bar"]}
1266+
)
1267+
1268+
def func(group_col):
1269+
return list(group_col.dropna().unique())
1270+
1271+
result = df.agg(func)
1272+
expected = pd.Series([[2, 3], [1.5], ["foo", "bar"]], index=["A", "B", "C"])
1273+
tm.assert_series_equal(result, expected)
1274+
1275+
result = df.agg([func])
1276+
expected = expected.to_frame("func").T
1277+
tm.assert_frame_equal(result, expected)
1278+
12621279
@pytest.mark.parametrize(
12631280
"df, func, expected",
12641281
chain(

0 commit comments

Comments
 (0)