Skip to content

Commit bef979b

Browse files
committed
PERF: Fix performance regression pandas-dev#33365
Fix performance regression in Series.is_monotonic_increasing for categorical by avoiding Categorical construction for categorical series
1 parent 455de19 commit bef979b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pandas/core/indexes/category.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
is_scalar,
2020
)
2121
from pandas.core.dtypes.dtypes import CategoricalDtype
22+
from pandas.core.dtypes.generic import ABCSeries
2223
from pandas.core.dtypes.missing import isna
2324

2425
from pandas.core import accessor
@@ -30,6 +31,7 @@
3031
from pandas.core.indexes.extension import ExtensionIndex, inherit_names
3132
import pandas.core.missing as missing
3233
from pandas.core.ops import get_op_result_name
34+
from pandas.core.construction import extract_array
3335

3436
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
3537
_index_doc_kwargs.update(dict(target_klass="CategoricalIndex"))
@@ -198,8 +200,14 @@ def __new__(
198200
data = []
199201

200202
assert isinstance(dtype, CategoricalDtype), dtype
201-
if not isinstance(data, Categorical) or data.dtype != dtype:
203+
if isinstance(data, (cls, ABCSeries)) and is_categorical_dtype(data):
204+
data = extract_array(data)
205+
206+
if not isinstance(data, Categorical):
202207
data = Categorical(data, dtype=dtype)
208+
elif isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
209+
# we want to silently ignore dtype='category'
210+
data = data._set_dtype(dtype)
203211

204212
data = data.copy() if copy else data
205213

0 commit comments

Comments
 (0)