BUG: reindex would throw when a categorical index was empty #16770 · rs2/pandas@3092bbc (original) (raw)
3 files changed
lines changed
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -42,6 +42,7 @@ Bug Fixes | ||
| 42 | 42 | - 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`) |
| 43 | 43 | - Fixed a bug in failing to compute rolling computations of a column-MultiIndexed ``DataFrame`` (:issue:`16789`, :issue:`16825`) |
| 44 | 44 | - 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`) | |
| 45 | 46 | |
| 46 | 47 | |
| 47 | 48 | Conversion |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -419,7 +419,11 @@ def reindex(self, target, method=None, level=None, limit=None, | ||
| 419 | 419 | raise ValueError("cannot reindex with a non-unique indexer") |
| 420 | 420 | |
| 421 | 421 | 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 | |
| 423 | 427 | |
| 424 | 428 | # filling in missing if needed |
| 425 | 429 | if len(missing): |
| @@ -430,7 +434,8 @@ def reindex(self, target, method=None, level=None, limit=None, | ||
| 430 | 434 | result = Index(np.array(self), name=self.name) |
| 431 | 435 | new_target, indexer, _ = result._reindex_non_unique( |
| 432 | 436 | np.array(target)) |
| 433 | - | |
| 437 | +# see GH 16819, indexer needs to be converted to correct type | |
| 438 | +indexer = np.array(indexer, dtype=np.int64) | |
| 434 | 439 | else: |
| 435 | 440 | |
| 436 | 441 | codes = new_target.codes.copy() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -419,6 +419,14 @@ def test_reindex_dtype(self): | ||
| 419 | 419 | tm.assert_numpy_array_equal(indexer, |
| 420 | 420 | np.array([0, 3, 2], dtype=np.int64)) |
| 421 | 421 | |
| 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 | + | |
| 422 | 430 | def test_duplicates(self): |
| 423 | 431 | |
| 424 | 432 | idx = CategoricalIndex([0, 0, 0], name='foo') |