Skip to content

Commit 2785007

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

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

pandas/core/indexes/category.py

Lines changed: 8 additions & 1 deletion
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 ABCCategorical, ABCSeries
2223
from pandas.core.dtypes.missing import isna
2324

2425
from pandas.core import accessor
@@ -198,8 +199,14 @@ def __new__(
198199
data = []
199200

200201
assert isinstance(dtype, CategoricalDtype), dtype
201-
if not isinstance(data, Categorical) or data.dtype != dtype:
202+
if isinstance(data, (cls, ABCSeries)) and is_categorical_dtype(data):
203+
data = data.values
204+
205+
if not isinstance(data, ABCCategorical):
202206
data = Categorical(data, dtype=dtype)
207+
elif isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
208+
# we want to silently ignore dtype='category'
209+
data = data._set_dtype(dtype)
203210

204211
data = data.copy() if copy else data
205212

0 commit comments

Comments
 (0)