Skip to content

Commit e13bcfe

Browse files
committed
BUG: get_group fails when multi-grouping with a categorical
1 parent 676cb95 commit e13bcfe

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

+10
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ Performance Improvements
5757

5858
Bug Fixes
5959
~~~~~~~~~
60+
61+
- Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`)
62+
63+
64+
- Bug in ``Timestamp``'s' ``microsecond``, ``quarter``, ``dayofyear``, ``week`` and ``daysinmonth`` properties return ``np.int`` type, not built-in ``int``. (:issue:`10050`)
65+
- Bug in ``GroupBy.get_group`` when grouping on multiple keys, one of which is categorical. (:issue:`10132`)
66+
- Bug in ``NaT`` raises ``AttributeError`` when accessing to ``daysinmonth``, ``dayofweek`` properties. (:issue:`10096`)
67+
68+
69+

pandas/core/groupby.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1301,11 +1301,18 @@ def apply(self, f, data, axis=0):
13011301
@cache_readonly
13021302
def indices(self):
13031303
""" dict {group name -> group indices} """
1304+
1305+
def extract_values(x):
1306+
if isinstance(x, CategoricalIndex):
1307+
return x.values.get_values()
1308+
else:
1309+
return _values_from_object(x)
1310+
13041311
if len(self.groupings) == 1:
13051312
return self.groupings[0].indices
13061313
else:
13071314
label_list = [ping.labels for ping in self.groupings]
1308-
keys = [_values_from_object(ping.group_index) for ping in self.groupings]
1315+
keys = [extract_values(ping.group_index) for ping in self.groupings]
13091316
return _get_indices_dict(label_list, keys)
13101317

13111318
@property

pandas/tests/test_groupby.py

+7
Original file line numberDiff line numberDiff line change
@@ -5140,6 +5140,13 @@ def test_groupby_categorical_two_columns(self):
51405140
"ints": [1,2,1,2,1,2]}).set_index(["cat","ints"])
51415141
tm.assert_frame_equal(res, exp)
51425142

5143+
# GH 10132
5144+
for key in [('a', 1), ('b', 2), ('b', 1), ('a', 2)]:
5145+
c, i = key
5146+
result = groups_double_key.get_group(key)
5147+
expected = test.query('cat == @c & ints == @i')
5148+
assert_frame_equal(result, expected)
5149+
51435150
d = {'C1': [3, 3, 4, 5], 'C2': [1, 2, 3, 4], 'C3': [10, 100, 200, 34]}
51445151
test = pd.DataFrame(d)
51455152
values = pd.cut(test['C1'], [1, 2, 3, 6])

0 commit comments

Comments
 (0)