Skip to content

Commit 5d8cc7a

Browse files
BUG: reindexing empty CategoricalIndex would fail if target had duplicates (pandas-dev#38906)
1 parent 8fe3dc6 commit 5d8cc7a

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Categorical
192192
- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`)
193193
- Bug where constructing a :class:`Categorical` from an object-dtype array of ``date`` objects did not round-trip correctly with ``astype`` (:issue:`38552`)
194194
- Bug in constructing a :class:`DataFrame` from an ``ndarray`` and a :class:`CategoricalDtype` (:issue:`38857`)
195+
- Bug where :class:`DataFrame` axes with an empty :class:`CategoricalIndex` could not be reindexed if the target contained duplicates (:issue:`38906`)
195196

196197
Datetimelike
197198
^^^^^^^^^^^^

pandas/core/indexes/base.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3745,9 +3745,13 @@ def _reindex_non_unique(self, target):
37453745
# need to retake to have the same size as the indexer
37463746
indexer[~check] = -1
37473747

3748-
# reset the new indexer to account for the new size
3749-
new_indexer = np.arange(len(self.take(indexer)))
3750-
new_indexer[~check] = -1
3748+
if len(self):
3749+
# reset the new indexer to account for the new size
3750+
new_indexer = np.arange(len(self.take(indexer)))
3751+
new_indexer[~check] = -1
3752+
else:
3753+
# GH#38906
3754+
new_indexer = np.arange(0)
37513755

37523756
if isinstance(self, ABCMultiIndex):
37533757
new_index = type(self).from_tuples(new_labels, names=self.names)

pandas/tests/frame/indexing/test_categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,4 @@ def test_loc_indexing_preserves_index_category_dtype(self):
385385
tm.assert_index_equal(result, expected)
386386

387387
result = df.loc[["a"]].index.levels[0]
388-
tm.assert_index_equal(result, expected)
388+
tm.assert_index_equal(result, expected)

pandas/tests/indexing/test_categorical.py

+17
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,20 @@ def test_loc_with_non_string_categories(self, idx_values, ordered):
533533
result.loc[sl, "A"] = ["qux", "qux2"]
534534
expected = DataFrame({"A": ["qux", "qux2", "baz"]}, index=cat_idx)
535535
tm.assert_frame_equal(result, expected)
536+
537+
def test_reindex_empty(self):
538+
df = pd.DataFrame(columns=pd.CategoricalIndex([]), index=['K'], dtype='f8')
539+
540+
# No duplicates
541+
cat_idx = pd.CategoricalIndex(['A', 'B'])
542+
result = df.reindex(columns=cat_idx)
543+
expected = DataFrame(index=['K'], columns=cat_idx, dtype='f8')
544+
tm.assert_frame_equal(result, expected)
545+
546+
# Duplicates
547+
# GH#38906
548+
cat_idx = pd.CategoricalIndex(['A', 'A'])
549+
result = df.reindex(columns=cat_idx)
550+
expected = DataFrame(index=['K'], columns=cat_idx, dtype='f8')
551+
tm.assert_frame_equal(result, expected)
552+

0 commit comments

Comments
 (0)