Skip to content

Commit 3092bbc

Browse files
committed
BUG: reindex would throw when a categorical index was empty #16770
1 parent 500cd0f commit 3092bbc

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

doc/source/whatsnew/v0.20.3.txt

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Bug Fixes
4242
- Fixed compat with loading a ``DataFrame`` with a ``PeriodIndex``, from a ``format='fixed'`` HDFStore, in Python 3, that was written in Python 2 (:issue:`16781`)
4343
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)
4444
- Bug in a DataFrame/Series with a ``TimedeltaIndex`` when slice indexing (:issue:`16637`)
45+
- Handle reindexing an empty categorical index rather than throwing (:issue:`16770`)
4546

4647

4748
Conversion

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
@@ -419,6 +419,14 @@ def test_reindex_dtype(self):
419419
tm.assert_numpy_array_equal(indexer,
420420
np.array([0, 3, 2], dtype=np.int64))
421421

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

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

0 commit comments

Comments
 (0)