Skip to content

Commit 99ebc4e

Browse files
Cast groupby tuple as list when multiindex
1 parent 881ee30 commit 99ebc4e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,10 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
27692769
keys = [key]
27702770
match_axis_length = False
27712771
else:
2772-
keys = key
2772+
if isinstance(key, tuple) and isinstance(obj._info_axis, MultiIndex):
2773+
keys = [key]
2774+
else:
2775+
keys = key
27732776
match_axis_length = len(keys) == len(group_axis)
27742777

27752778
# what are we after, exactly?

pandas/tests/groupby/test_groupby.py

+19
Original file line numberDiff line numberDiff line change
@@ -3063,6 +3063,25 @@ def test_groupby_non_arithmetic_agg_types(self):
30633063
t = getattr(grpd, method)(*data['args'])
30643064
assert_frame_equal(t, df_out)
30653065

3066+
def test_groupby_multiindex_tuple(self):
3067+
# GH 17979
3068+
df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
3069+
columns=pd.MultiIndex.from_arrays(
3070+
[['a', 'b', 'b', 'c'],
3071+
[1, 1, 2, 2]]))
3072+
expected = df.groupby([('b', 1)]).groups
3073+
result = df.groupby(('b', 1)).groups
3074+
tm.assert_dict_equal(expected, result)
3075+
3076+
df2 = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
3077+
columns=pd.MultiIndex.from_arrays(
3078+
[['a', 'b', 'b', 'c'],
3079+
['d', 'd', 'e', 'e']]))
3080+
df2.groupby([('b', 'd')]).groups
3081+
expected = df.groupby([('b', 'd')]).groups
3082+
result = df.groupby(('b', 'd')).groups
3083+
tm.assert_dict_equal(expected, result)
3084+
30663085
def test_groupby_non_arithmetic_agg_intlike_precision(self):
30673086
# GH9311, GH6620
30683087
c = 24650000000000000

0 commit comments

Comments
 (0)