Skip to content

Commit 94266d4

Browse files
PERF: Faster CategoricalIndex from categorical (#17513)
1 parent ad70ed4 commit 94266d4

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ Performance Improvements
469469
- :attr:`Series.dt` no longer performs frequency inference, yielding a large speedup when accessing the attribute (:issue:`17210`)
470470
- Improved performance of :meth:`Categorical.set_categories` by not materializing the values (:issue:`17508`)
471471
- :attr:`Timestamp.microsecond` no longer re-computes on attribute access (:issue:`17331`)
472+
- Improved performance of the :class:`CategoricalIndex` for data that is already categorical dtype (:issue:`17513`)
472473

473474
.. _whatsnew_0210.bug_fixes:
474475

pandas/core/indexes/category.py

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def _create_categorical(self, data, categories=None, ordered=None):
130130
-------
131131
Categorical
132132
"""
133+
if (isinstance(data, (ABCSeries, type(self))) and
134+
is_categorical_dtype(data)):
135+
data = data.values
136+
133137
if not isinstance(data, ABCCategorical):
134138
ordered = False if ordered is None else ordered
135139
from pandas.core.categorical import Categorical

pandas/tests/indexes/test_category.py

+10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ def test_construction_with_dtype(self):
125125
result = CategoricalIndex(idx, categories=idx, ordered=True)
126126
tm.assert_index_equal(result, expected, exact=True)
127127

128+
def test_create_categorical(self):
129+
# https://github.com/pandas-dev/pandas/pull/17513
130+
# The public CI constructor doesn't hit this code path with
131+
# instances of CategoricalIndex, but we still want to test the code
132+
ci = CategoricalIndex(['a', 'b', 'c'])
133+
# First ci is self, second ci is data.
134+
result = CategoricalIndex._create_categorical(ci, ci)
135+
expected = Categorical(['a', 'b', 'c'])
136+
tm.assert_categorical_equal(result, expected)
137+
128138
def test_disallow_set_ops(self):
129139

130140
# GH 10039

0 commit comments

Comments
 (0)