Skip to content

REGR: fix DataFrame.agg case with list-like return value #29632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,9 +602,9 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis):
if not len(results):
raise ValueError("no results")

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

# we are concatting non-NDFrame objects,
# e.g. a list of scalars
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,23 @@ def test_non_callable_aggregates(self):

assert result == expected

def test_agg_listlike_result(self):
# GH-29587 user defined function returning list-likes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we say something snide about this user? i dislike this hypothetical user.

df = DataFrame(
{"A": [2, 2, 3], "B": [1.5, np.nan, 1.5], "C": ["foo", None, "bar"]}
)

def func(group_col):
return list(group_col.dropna().unique())

result = df.agg(func)
expected = pd.Series([[2, 3], [1.5], ["foo", "bar"]], index=["A", "B", "C"])
tm.assert_series_equal(result, expected)

result = df.agg([func])
expected = expected.to_frame("func").T
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"df, func, expected",
chain(
Expand Down