Skip to content

Commit

Permalink
BUG: reindex would throw when a categorical index was empty #16770
Browse files Browse the repository at this point in the history
closes #16770

Author: ri938 <r_irv938@hotmail.com>
Author: Jeff Reback <jeff@reback.net>
Author: Tuan <tuan.d.tran@hotmail.com>
Author: Forbidden Donut <forbdonut@gmail.com>

This patch had conflicts when merged, resolved by
Committer: Jeff Reback <jeff@reback.net>

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
  • Loading branch information
ri938 authored and jreback committed Jul 18, 2017
1 parent a1dfb03 commit 01d7be5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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 @@ -192,6 +192,7 @@ Indexing
- Fixed :func:`TimedeltaIndex.get_loc` handling of ``np.timedelta64`` inputs (:issue:`16909`).
- 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`).
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)
- Bug in reindexing on an empty ``CategoricalIndex`` (:issue:`16770`)

I/O
^^^
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):
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

0 comments on commit 01d7be5

Please sign in to comment.