Skip to content

Commit 4c0727e

Browse files
authored
Merge pull request #2 from CSCD01-team14/bugfix/get_group
fixed get_group as per issue#32492
2 parents 09a46a4 + 449cf63 commit 4c0727e

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ Plotting
312312
-
313313
- Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`)
314314

315+
Groupby
316+
^^^^^^^
317+
318+
- Bug in :meth:`GroupBy.get_group` raises ``KeyError`` when a ``name`` that does not exist is searched for (:issue:`32492`)
319+
315320

316321
Groupby/resample/rolling
317322
^^^^^^^^^^^^^^^^^^^^^^^^
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# DO NOT MERGE _test_get_groups.py WITH ACTUAL PANDAS
2+
# DO NOT MERGE _test_get_groups.py WITH ACTUAL PANDAS
3+
# DO NOT MERGE _test_get_groups.py WITH ACTUAL PANDAS
4+
5+
import unittest
6+
7+
from pandas import DataFrame
8+
from pandas.testing import assert_frame_equal
9+
import numpy as np
10+
11+
class _test_get_groups(unittest.TestCase):
12+
def test_discovered_invalid(self):
13+
df = DataFrame(data={
14+
'A': ['a1', 'a2', None],
15+
'B': ['b1', 'b2', 'b1'],
16+
'val': [1, 2, 3],
17+
})
18+
grps = df.groupby(by=['A', 'B'])
19+
self.assertRaises(KeyError, grps.get_group, ('a1', 'b2'))
20+
21+
def test_single_row_valid(self):
22+
df = DataFrame(data={
23+
'A': ['a1', 'a2', None],
24+
'B': ['b1', 'b2', 'b1'],
25+
'val': [1, 2, 3],
26+
})
27+
expected = DataFrame(data={
28+
'A': ['a1'],
29+
'B': ['b1'],
30+
'val': [1,],
31+
})
32+
grps = df.groupby(by=['A', 'B'])
33+
assert_frame_equal(grps.get_group(('a1', 'b1')), expected)
34+
35+
def test_alternate_invalid(self):
36+
df = DataFrame(data={
37+
'A': ['a1', 'a2', None],
38+
'B': ['b1', 'b2', 'b1'],
39+
'val': [1, 2, 3],
40+
})
41+
grps = df.groupby(by=['A', 'B'])
42+
self.assertRaises(KeyError, grps.get_group, ('a2', 'b1'))
43+
44+
def test_double_row_valid(self):
45+
df = DataFrame(data={
46+
'A': ['a1', 'a2', None],
47+
'B': ['b1', 'b2', 'b1'],
48+
'val': [1, 2, 3],
49+
})
50+
expected = DataFrame(data={
51+
'A': ['a1', None],
52+
'B': ['b1', 'b1'],
53+
'val': [1, 3],
54+
})
55+
grps = df.groupby(by=['B'])
56+
self.assertEqual(True, np.array_equal(grps.get_group(('b1')).values, expected.values))
57+
58+
def test_expect_empty(self):
59+
df = DataFrame(data={
60+
'A': ['a1', 'a2', None],
61+
'B': ['b1', 'b2', 'b1'],
62+
'val': [1, 2, 3],
63+
})
64+
grps = df.groupby(by=['B'])
65+
self.assertRaises(KeyError, grps.get_group, ('b3'))
66+
67+
def test_double_row_valid(self):
68+
df = DataFrame({'Animal': ['Falcon', 'Falcon',
69+
'Parrot', 'Parrot'],
70+
'Max Speed': [380., 370., 24., 26.],
71+
'bullshit': ['a', 'b', 'c', 'd']})
72+
expected = DataFrame({'Animal': ['Falcon', 'Falcon'],
73+
'Max Speed': [380., 370.],
74+
'bullshit': ['a', 'b']})
75+
grps = df.groupby(['Animal'])
76+
assert_frame_equal(grps.get_group(('Falcon')), expected)
77+
78+
def test_multi_group_multi_result(self):
79+
df = DataFrame([('bird', 'Falconiformes', 389.0),
80+
('bird', 'Psittaciformes', 24.0),
81+
('mammal', 'Carnivora', 80.2),
82+
('mammal', 'Primates', None),
83+
('mammal', 'Carnivora', 58)],
84+
index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'],
85+
columns=('class', 'order', 'max_speed'))
86+
expected = DataFrame([('mammal', 'Carnivora', 80.2),
87+
('mammal', 'Carnivora', 58)],
88+
index=['lion', 'leopard'],
89+
columns=('class', 'order', 'max_speed'))
90+
grps = df.groupby(['class', 'order']);
91+
assert_frame_equal(grps.get_group(('mammal','Carnivora')), expected)
92+
93+
def test_single_group_no_tuple(self):
94+
df = DataFrame([('bird', 'Falconiformes', 389.0),
95+
('bird', 'Psittaciformes', 24.0),
96+
('mammal', 'Carnivora', 80.2),
97+
('mammal', 'Primates', None),
98+
('mammal', 'Carnivora', 58)],
99+
index=['falcon', 'parrot', 'lion', 'monkey', 'leopard'],
100+
columns=('class', 'order', 'max_speed'))
101+
expected = DataFrame([('bird', 'Falconiformes', 389.0),
102+
('bird', 'Psittaciformes', 24.0)],
103+
index=['falcon', 'parrot'],
104+
columns=('class', 'order', 'max_speed'))
105+
grps = df.groupby(['class']);
106+
assert_frame_equal(grps.get_group('bird'), expected)
107+
108+
if __name__ == '__main__':
109+
unittest.main()

pandas/core/groupby/groupby.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,25 @@ def get_group(self, name, obj=None):
685685
if not len(inds):
686686
raise KeyError(name)
687687

688+
try:
689+
isValid = True
690+
for index in inds:
691+
for i in range(len(self.keys)):
692+
key = self.keys[i]
693+
# if the cell does not contain the searched for data
694+
if (type(name) != tuple):
695+
if obj.iloc[index, :][key] != name:
696+
isValid = False
697+
elif obj.iloc[index, :][key] != name[i]:
698+
isValid = False
699+
else:
700+
isValid = isValid and True
701+
except TypeError:
702+
pass
703+
else:
704+
if not isValid:
705+
raise KeyError(name)
706+
688707
return obj._take_with_is_copy(inds, axis=self.axis)
689708

690709
def __iter__(self):

0 commit comments

Comments
 (0)