Skip to content
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

BUG: reindex would throw when a categorical index was empty #16770 #16820

Closed
wants to merge 18 commits 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.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Bug Fixes
~~~~~~~~~

- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
- Bug in reindexing on an empty ``CategoricalIndex`` (:issue:`16770`)
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)

Conversion
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,11 @@ def reindex(self, target, method=None, level=None, limit=None,
raise ValueError("cannot reindex with a non-unique indexer")

indexer, missing = self.get_indexer_non_unique(np.array(target))
new_target = self.take(indexer)

if len(self.codes):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if len(indexer)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case the target is [1] say and the categorical index is empty then indexer here will be [-1] so not empty. The error occurs when try to run take on the empty underlying index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right its missing, so ok then.

new_target = self.take(indexer)
else:
new_target = target

# filling in missing if needed
if len(missing):
Expand All @@ -430,7 +434,6 @@ def reindex(self, target, method=None, level=None, limit=None,
result = Index(np.array(self), name=self.name)
new_target, indexer, _ = result._reindex_non_unique(
np.array(target))

else:

codes = new_target.codes.copy()
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,14 @@ def test_reindex_dtype(self):
tm.assert_numpy_array_equal(indexer,
np.array([0, 3, 2], dtype=np.intp))

def test_reindex_empty_index(self):
# See GH16770
c = CategoricalIndex([])
res, indexer = c.reindex(['a', 'b'])
tm.assert_index_equal(res, Index(['a', 'b']), exact=True)
tm.assert_numpy_array_equal(indexer,
np.array([-1, -1], dtype=np.intp))

def test_duplicates(self):

idx = CategoricalIndex([0, 0, 0], name='foo')
Expand Down