Skip to content

Commit 6b53334

Browse files
committed
ENH: handle mixed multiple function spec in SeriesGroupBy.aggregate, close #1359
1 parent d464f75 commit 6b53334

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pandas/core/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,10 @@ def _aggregate_multiple_funcs(self, arg):
12021202
if isinstance(arg, dict):
12031203
columns = arg.keys()
12041204
arg = arg.items()
1205-
elif isinstance(arg[0], (tuple, list)):
1205+
elif any(isinstance(x, (tuple, list)) for x in arg):
1206+
arg = [(x, x) if not isinstance(x, (tuple, list)) else x
1207+
for x in arg]
1208+
12061209
# indicated column order
12071210
columns = list(zip(*arg))[0]
12081211
else:

pandas/tests/test_groupby.py

+14
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,20 @@ def test_agg_multiple_functions_maintain_order(self):
17761776

17771777
self.assert_(np.array_equal(result.columns, exp_cols))
17781778

1779+
def test_multiple_functions_tuples_and_non_tuples(self):
1780+
# #1359
1781+
1782+
funcs = [('foo', 'mean'), 'std']
1783+
ex_funcs = [('foo', 'mean'), ('std', 'std')]
1784+
1785+
result = self.df.groupby('A')['C'].agg(funcs)
1786+
expected = self.df.groupby('A')['C'].agg(ex_funcs)
1787+
assert_frame_equal(result, expected)
1788+
1789+
result = self.df.groupby('A').agg(funcs)
1790+
expected = self.df.groupby('A').agg(ex_funcs)
1791+
assert_frame_equal(result, expected)
1792+
17791793
def test_more_flexible_frame_multi_function(self):
17801794
from pandas import concat
17811795

0 commit comments

Comments
 (0)