Skip to content

Commit 6efa813

Browse files
author
Tom Augspurger
committed
Merge pull request #8123 from TomAugspurger/groupby-tuple
BUG: fix groupby with tuple bug
2 parents 62663b4 + 110f10c commit 6efa813

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

doc/source/v0.15.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ Bug Fixes
640640

641641

642642

643-
643+
- Bug in accessing groups from a ``GroupBy`` when the original grouper
644+
was a tuple (:issue:`8121`).
644645

645646

pandas/core/groupby.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,18 @@ def convert(key, s):
431431
sample = next(iter(self.indices))
432432
if isinstance(sample, tuple):
433433
if not isinstance(name, tuple):
434-
raise ValueError("must supply a tuple to get_group with multiple grouping keys")
434+
msg = ("must supply a tuple to get_group with multiple"
435+
" grouping keys")
436+
raise ValueError(msg)
435437
if not len(name) == len(sample):
436-
raise ValueError("must supply a a same-length tuple to get_group with multiple grouping keys")
438+
try:
439+
# If the original grouper was a tuple
440+
return self.indices[name]
441+
except KeyError:
442+
# turns out it wasn't a tuple
443+
msg = ("must supply a a same-length tuple to get_group"
444+
" with multiple grouping keys")
445+
raise ValueError(msg)
437446

438447
name = tuple([ convert(n, k) for n, k in zip(name,sample) ])
439448

pandas/tests/test_groupby.py

+17
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,23 @@ def test_get_group(self):
598598
self.assertRaises(ValueError, lambda : g.get_group(('foo')))
599599
self.assertRaises(ValueError, lambda : g.get_group(('foo','bar','baz')))
600600

601+
def test_get_group_grouped_by_tuple(self):
602+
# GH 8121
603+
df = DataFrame([[(1,), (1, 2), (1,), (1, 2)]],
604+
index=['ids']).T
605+
gr = df.groupby('ids')
606+
expected = DataFrame({'ids': [(1,), (1,)]}, index=[0, 2])
607+
result = gr.get_group((1,))
608+
assert_frame_equal(result, expected)
609+
610+
dt = pd.to_datetime(['2010-01-01', '2010-01-02', '2010-01-01',
611+
'2010-01-02'])
612+
df = DataFrame({'ids': [(x,) for x in dt]})
613+
gr = df.groupby('ids')
614+
result = gr.get_group(('2010-01-01',))
615+
expected = DataFrame({'ids': [(dt[0],), (dt[0],)]}, index=[0, 2])
616+
assert_frame_equal(result, expected)
617+
601618
def test_agg_apply_corner(self):
602619
# nothing to group, all NA
603620
grouped = self.ts.groupby(self.ts * np.nan)

0 commit comments

Comments
 (0)