Skip to content

BUG: loc-indexing on a CategoricalIndex with integer categories #29859

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
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/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ Indexing
- Fix assignment of column via `.loc` with numpy non-ns datetime type (:issue:`27395`)
- Bug in :meth:`Float64Index.astype` where ``np.inf`` was not handled properly when casting to an integer dtype (:issue:`28475`)
- :meth:`Index.union` could fail when the left contained duplicates (:issue:`28257`)
- Bug when indexing with ``.loc`` and the index is a :class:`CateggoricalIndex` with integer categories (:issue:`17569`)
- :meth:`Index.get_indexer_non_unique` could fail with `TypeError` in some cases, such as when searching for ints in a string index (:issue:`28257`)
- Bug in :meth:`Float64Index.get_loc` incorrectly raising ``TypeError`` instead of ``KeyError`` (:issue:`29189`)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def get_indexer_non_unique(self, target):

@Appender(_index_shared_docs["_convert_scalar_indexer"])
def _convert_scalar_indexer(self, key, kind=None):
if self.categories._defer_to_indexing:
if kind == "loc" or self.categories._defer_to_indexing:
return self.categories._convert_scalar_indexer(key, kind=kind)

return super()._convert_scalar_indexer(key, kind=kind)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,26 @@ def test_map_with_dict_or_series(self):
output = cur_index.map(mapper)
# Order of categories in output can be different
tm.assert_index_equal(expected, output)

def test_indexing_with_integer_categories(self):
# GH-17569
cat_idx = CategoricalIndex([1, 2, 3])
cat = DataFrame({"A": ["foo", "bar", "baz"]}, index=cat_idx)
# scalar
result = cat.loc[1]
expected = Series(["foo"], index=["A"], name=1)
tm.assert_series_equal(result, expected)
# list
result = cat.loc[[1, 2]]
expected = DataFrame(["foo", "bar"], index=cat_idx[:2], columns=["A"])
tm.assert_frame_equal(result, expected)
# scalar assignment
result = cat.copy()
result.loc[1] = "qux"
expected = DataFrame({"A": ["qux", "bar", "baz"]}, index=cat_idx)
tm.assert_frame_equal(result, expected)
# list assignment
result = cat.copy()
result.loc[[1, 2], "A"] = ["qux", "qux2"]
expected = DataFrame({"A": ["qux", "qux2", "baz"]}, index=cat_idx)
tm.assert_frame_equal(result, expected)