Skip to content

Commit 114e33a

Browse files
authored
PERF: Fix performance regression #33365 (#33540)
Fix performance regression in Series.is_monotonic_increasing for categorical by avoiding Categorical construction for categorical series
1 parent d106b81 commit 114e33a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

pandas/core/indexes/category.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pandas.core.algorithms import take_1d
2626
from pandas.core.arrays.categorical import Categorical, contains, recode_for_categories
2727
import pandas.core.common as com
28+
from pandas.core.construction import extract_array
2829
import pandas.core.indexes.base as ibase
2930
from pandas.core.indexes.base import Index, _index_shared_docs, maybe_extract_name
3031
from pandas.core.indexes.extension import ExtensionIndex, inherit_names
@@ -198,8 +199,13 @@ def __new__(
198199
data = []
199200

200201
assert isinstance(dtype, CategoricalDtype), dtype
201-
if not isinstance(data, Categorical) or data.dtype != dtype:
202+
data = extract_array(data, extract_numpy=True)
203+
204+
if not isinstance(data, Categorical):
202205
data = Categorical(data, dtype=dtype)
206+
elif isinstance(dtype, CategoricalDtype) and dtype != data.dtype:
207+
# we want to silently ignore dtype='category'
208+
data = data._set_dtype(dtype)
203209

204210
data = data.copy() if copy else data
205211

0 commit comments

Comments
 (0)