Skip to content

Commit

Permalink
BUG: reindex would throw when a categorical index was empty pandas-de…
Browse files Browse the repository at this point in the history
  • Loading branch information
ri938 committed Jul 7, 2017
1 parent 500cd0f commit 3092bbc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.20.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Bug Fixes
- 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`)
- Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`)
- Bug in a DataFrame/Series with a ``TimedeltaIndex`` when slice indexing (:issue:`16637`)
- Handle reindexing an empty categorical index rather than throwing (:issue:`16770`)


Conversion
Expand Down
9 changes: 7 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,8 @@ 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))

# see GH 16819, indexer needs to be converted to correct type
indexer = np.array(indexer, dtype=np.int64)
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.int64))

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.int64))

def test_duplicates(self):

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

0 comments on commit 3092bbc

Please sign in to comment.