Skip to content

Bug in groupby.get_group on categoricalindex #15163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Bug Fixes

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



Expand Down
3 changes: 3 additions & 0 deletions pandas/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ def categories(self):
def ordered(self):
return self._data.ordered

def _reverse_indexer(self):
return self._data._reverse_indexer()

def __contains__(self, key):
hash(key)
return key in self.values
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ def setUp(self):
'E': np.random.randn(11),
'F': np.random.randn(11)})

def test_level_groupby_get_group(self):
# gh15155
test = DataFrame(data=np.arange(2, 22, 2),
index=MultiIndex(levels=[pd.CategoricalIndex(
["a", "b"]), range(10)],
labels=[[0] * 5 + [1] * 5, range(10)],
names=["Index1", "Index2"]))
testGroupedBy = test.groupby(level=["Index1"])
# expected should equal test.loc[["a"]], but does not - see gh15166
expected = DataFrame(data=np.arange(2, 12, 2),
index=pd.MultiIndex(levels=[pd.CategoricalIndex(
["a", "b"]), range(5)],
labels=[[0] * 5, range(5)],
names=["Index1", "Index2"]))
assert_frame_equal(testGroupedBy.get_group("a"), expected)

def test_apply_use_categorical_name(self):
from pandas import qcut
cats = qcut(self.df.C, 4)
Expand Down