From 3092bbce60a58723cb0c61719c87ff1924e9d032 Mon Sep 17 00:00:00 2001 From: ri938 Date: Mon, 3 Jul 2017 21:10:28 +0100 Subject: [PATCH] BUG: reindex would throw when a categorical index was empty #16770 --- doc/source/whatsnew/v0.20.3.txt | 1 + pandas/core/indexes/category.py | 9 +++++++-- pandas/tests/indexes/test_category.py | 8 ++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.20.3.txt b/doc/source/whatsnew/v0.20.3.txt index 3d6aba98d4d57..3d82b2bd452bb 100644 --- a/doc/source/whatsnew/v0.20.3.txt +++ b/doc/source/whatsnew/v0.20.3.txt @@ -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 diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index d13636e8b43e2..7d3d96311f05a 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -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): @@ -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() diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py index 493274fff43e0..e1ac811b90bbc 100644 --- a/pandas/tests/indexes/test_category.py +++ b/pandas/tests/indexes/test_category.py @@ -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')