diff --git a/doc/source/v0.15.0.txt b/doc/source/v0.15.0.txt index 95bf2918f8992..1bfc41fa0bdb5 100644 --- a/doc/source/v0.15.0.txt +++ b/doc/source/v0.15.0.txt @@ -640,6 +640,7 @@ Bug Fixes - +- Bug in accessing groups from a ``GroupBy`` when the original grouper + was a tuple (:issue:`8121`). diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index eaaf85a1f5f84..afca50af92be5 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -431,9 +431,18 @@ def convert(key, s): sample = next(iter(self.indices)) if isinstance(sample, tuple): if not isinstance(name, tuple): - raise ValueError("must supply a tuple to get_group with multiple grouping keys") + msg = ("must supply a tuple to get_group with multiple" + " grouping keys") + raise ValueError(msg) if not len(name) == len(sample): - raise ValueError("must supply a a same-length tuple to get_group with multiple grouping keys") + try: + # If the original grouper was a tuple + return self.indices[name] + except KeyError: + # turns out it wasn't a tuple + msg = ("must supply a a same-length tuple to get_group" + " with multiple grouping keys") + raise ValueError(msg) name = tuple([ convert(n, k) for n, k in zip(name,sample) ]) diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 5d087a1ae0810..0e9b2030b10e3 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -598,6 +598,23 @@ def test_get_group(self): self.assertRaises(ValueError, lambda : g.get_group(('foo'))) self.assertRaises(ValueError, lambda : g.get_group(('foo','bar','baz'))) + def test_get_group_grouped_by_tuple(self): + # GH 8121 + df = DataFrame([[(1,), (1, 2), (1,), (1, 2)]], + index=['ids']).T + gr = df.groupby('ids') + expected = DataFrame({'ids': [(1,), (1,)]}, index=[0, 2]) + result = gr.get_group((1,)) + assert_frame_equal(result, expected) + + dt = pd.to_datetime(['2010-01-01', '2010-01-02', '2010-01-01', + '2010-01-02']) + df = DataFrame({'ids': [(x,) for x in dt]}) + gr = df.groupby('ids') + result = gr.get_group(('2010-01-01',)) + expected = DataFrame({'ids': [(dt[0],), (dt[0],)]}, index=[0, 2]) + assert_frame_equal(result, expected) + def test_agg_apply_corner(self): # nothing to group, all NA grouped = self.ts.groupby(self.ts * np.nan)