Skip to content

Commit 01d7be5

Browse files
ri938jreback
authored andcommitted
BUG: reindex would throw when a categorical index was empty #16770
closes #16770 Author: ri938 <[email protected]> Author: Jeff Reback <[email protected]> Author: Tuan <[email protected]> Author: Forbidden Donut <[email protected]> This patch had conflicts when merged, resolved by Committer: Jeff Reback <[email protected]> Closes #16820 from ri938/bug_issue16770 and squashes the following commits: 0e2d315 [ri938] Merge branch 'master' into bug_issue16770 9802288 [ri938] Update v0.20.3.txt 1f2865e [ri938] Update v0.20.3.txt 83fd749 [ri938] Update v0.20.3.txt eab3192 [ri938] Merge branch 'master' into bug_issue16770 7acc09f [ri938] Minor correction to previous submit 6e8f1b3 [ri938] Minor corrections to previous submit (#16820) 9ed80f0 [ri938] Bring documentation into line with master branch. 26e1a60 [ri938] Move documentation of change to the next major release 0.21.0 59b17cd [Jeff Reback] BUG: rolling.cov with multi-index columns should presever the MI (#16825) 5362447 [Tuan] fix BUG: ValueError when performing rolling covariance on multi indexed DataFrame (#16814) 800b40d [ri938] BUG: render dataframe as html do not produce duplicate element id's (#16780) (#16801) a725fbf [Forbidden Donut] BUG: Fix read of py3 PeriodIndex DataFrame HDF made in py2 (#16781) (#16790) 8f8e3d6 [ri938] TST: register slow marker (#16797) 0645868 [ri938] Add backticks in documentation 0a20024 [ri938] Minor correction to previous submit 69454ec [ri938] Minor corrections to previous submit (#16820) 3092bbc [ri938] BUG: reindex would throw when a categorical index was empty #16770
1 parent a1dfb03 commit 01d7be5

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Indexing
192192
- Fixed :func:`TimedeltaIndex.get_loc` handling of ``np.timedelta64`` inputs (:issue:`16909`).
193193
- Fix :func:`MultiIndex.sort_index` ordering when ``ascending`` argument is a list, but not all levels are specified, or are in a different order (:issue:`16934`).
194194
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)
195+
- Bug in reindexing on an empty ``CategoricalIndex`` (:issue:`16770`)
195196

196197
I/O
197198
^^^

pandas/core/indexes/category.py

+5-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,6 @@ 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-
434437
else:
435438

436439
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.intp))
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.intp))
429+
422430
def test_duplicates(self):
423431

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

0 commit comments

Comments
 (0)