Skip to content

Commit e9cfd35

Browse files
committed
BUG: groupby with dict passed had stopped working in some cases, GH #679
1 parent dfd056b commit e9cfd35

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ def _get_groupings(obj, grouper=None, axis=0, level=None, sort=True):
688688

689689
# what are we after, exactly?
690690
match_axis_length = len(groupers) == len(group_axis)
691-
any_callable = any(callable(g) for g in groupers)
691+
any_callable = any(callable(g) or isinstance(g, dict) for g in groupers)
692692
any_arraylike = any(isinstance(g, (list, tuple, np.ndarray))
693693
for g in groupers)
694694

pandas/tests/test_groupby.py

+19
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ def test_basic(self):
120120
# corner cases
121121
self.assertRaises(Exception, grouped.aggregate, lambda x: x * 2)
122122

123+
def test_groupby_dict_mapping(self):
124+
# GH #679
125+
from pandas import Series
126+
s = Series({'T1': 5})
127+
result = s.groupby({'T1': 'T2'}).agg(sum)
128+
expected = s.groupby(['T2']).agg(sum)
129+
assert_series_equal(result, expected)
130+
131+
s = Series([1., 2., 3., 4.], index=list('abcd'))
132+
mapping = {'a' : 0, 'b' : 0, 'c' : 1, 'd' : 1}
133+
134+
result = s.groupby(mapping).mean()
135+
result2 = s.groupby(mapping).agg(np.mean)
136+
expected = s.groupby([0, 0, 1, 1]).mean()
137+
expected2 = s.groupby([0, 0, 1, 1]).mean()
138+
assert_series_equal(result, expected)
139+
assert_series_equal(result, result2)
140+
assert_series_equal(result, expected2)
141+
123142
def test_groupby_nonobject_dtype(self):
124143
key = self.mframe.index.labels[0]
125144
grouped = self.mframe.groupby(key)

0 commit comments

Comments
 (0)