Skip to content

Commit 742d4a5

Browse files
committed
BUG: GroupBy.get_group failing with a categorical grouper (pandas-dev#15155)
1 parent 684c4d5 commit 742d4a5

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

doc/source/whatsnew/v0.20.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ Bug Fixes
389389

390390
- Bug in compat for passing long integers to ``Timestamp.replace`` (:issue:`15030`)
391391
- Bug in ``.loc`` that would not return the correct dtype for scalar access for a DataFrame (:issue:`11617`)
392+
- Bug in ``GroupBy.get_group`` failing with a categorical grouper (:issue:`15155`)
392393

393394

394395

pandas/indexes/category.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ def categories(self):
255255
def ordered(self):
256256
return self._data.ordered
257257

258+
def _reverse_indexer(self):
259+
return self._data._reverse_indexer()
260+
258261
def __contains__(self, key):
259262
hash(key)
260263
return key in self.values

pandas/tests/groupby/test_categorical.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ def setUp(self):
6767
'E': np.random.randn(11),
6868
'F': np.random.randn(11)})
6969

70+
def test_level_groupby_get_group(self):
71+
# gh15155
72+
test = DataFrame(data=np.arange(2, 22, 2),
73+
index=MultiIndex(levels=[pd.CategoricalIndex(
74+
["a", "b"]), range(10)],
75+
labels=[[0] * 5 + [1] * 5, range(10)],
76+
names=["Index1", "Index2"]))
77+
testGroupedBy = test.groupby(level=["Index1"])
78+
# expected should equal test.loc[["a"]], but does not - see gh15166
79+
expected = DataFrame(data=np.arange(2, 12, 2),
80+
index=pd.MultiIndex(levels=[pd.CategoricalIndex(
81+
["a", "b"]), range(5)],
82+
labels=[[0] * 5, range(5)],
83+
names=["Index1", "Index2"]))
84+
assert_frame_equal(testGroupedBy.get_group("a"), expected)
85+
7086
def test_apply_use_categorical_name(self):
7187
from pandas import qcut
7288
cats = qcut(self.df.C, 4)

0 commit comments

Comments
 (0)