Skip to content

Commit df5bfcf

Browse files
committed
BUG: reindex would throw when a categorical index was empty pandas-dev#16770
1 parent 664348c commit df5bfcf

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Performance Improvements
3737
Bug Fixes
3838
~~~~~~~~~
3939
- Fixed issue with dataframe scatter plot for categorical data that reports incorrect column key not found when categorical data is used for plotting (:issue:`16199`)
40-
40+
- Handle reindexing an empty categorical index rather than throwing (:issue:`16770`)
4141

4242

4343

pandas/core/indexes/category.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,11 @@ def reindex(self, target, method=None, level=None, limit=None,
419419
raise ValueError("cannot reindex with a non-unique indexer")
420420

421421
indexer, missing = self.get_indexer_non_unique(np.array(target))
422-
new_target = self.take(indexer)
422+
423+
if len(self.codes):
424+
new_target = self.take(indexer)
425+
else:
426+
new_target = target
423427

424428
# filling in missing if needed
425429
if len(missing):
@@ -430,7 +434,8 @@ def reindex(self, target, method=None, level=None, limit=None,
430434
result = Index(np.array(self), name=self.name)
431435
new_target, indexer, _ = result._reindex_non_unique(
432436
np.array(target))
433-
437+
# see GH 16819, indexer needs to be converted to correct type
438+
indexer = np.array(indexer, dtype=np.int64)
434439
else:
435440

436441
codes = new_target.codes.copy()

pandas/tests/indexes/test_category.py

+8
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ def test_reindex_dtype(self):
420420
tm.assert_numpy_array_equal(indexer,
421421
np.array([0, 3, 2], dtype=np.int64))
422422

423+
def test_reindex_empty_index(self):
424+
# See GH16770
425+
c = CategoricalIndex([])
426+
res, indexer = c.reindex(['a', 'b'])
427+
tm.assert_index_equal(res, Index(['a', 'b']), exact=True)
428+
tm.assert_numpy_array_equal(indexer,
429+
np.array([-1, -1], dtype=np.int64))
430+
423431
def test_duplicates(self):
424432

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

0 commit comments

Comments
 (0)