Skip to content

Commit 85be99e

Browse files
authored
PERF: CategoricalDtype.update_dtype (pandas-dev#59647)
* PERF: CategoricalDtype.update_dtype * Add whatsnew number add comment * Fix unit test * short circut only for the dtype
1 parent bc9b1c3 commit 85be99e

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ Performance improvements
528528
- Performance improvement in :meth:`RangeIndex.reindex` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57647`, :issue:`57752`)
529529
- Performance improvement in :meth:`RangeIndex.take` returning a :class:`RangeIndex` instead of a :class:`Index` when possible. (:issue:`57445`, :issue:`57752`)
530530
- Performance improvement in :func:`merge` if hash-join can be used (:issue:`57970`)
531+
- Performance improvement in :meth:`CategoricalDtype.update_dtype` when ``dtype`` is a :class:`CategoricalDtype` with non ``None`` categories and ordered (:issue:`59647`)
531532
- Performance improvement in :meth:`to_hdf` avoid unnecessary reopenings of the HDF5 file to speedup data addition to files with a very large number of groups . (:issue:`58248`)
532533
- Performance improvement in ``DataFrameGroupBy.__len__`` and ``SeriesGroupBy.__len__`` (:issue:`57595`)
533534
- Performance improvement in indexing operations for string dtypes (:issue:`56997`)

pandas/core/dtypes/dtypes.py

+7
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ def update_dtype(self, dtype: str_type | CategoricalDtype) -> CategoricalDtype:
611611
dtype = cast(CategoricalDtype, dtype)
612612

613613
# update categories/ordered unless they've been explicitly passed as None
614+
if (
615+
isinstance(dtype, CategoricalDtype)
616+
and dtype.categories is not None
617+
and dtype.ordered is not None
618+
):
619+
# Avoid re-validation in CategoricalDtype constructor
620+
return dtype
614621
new_categories = (
615622
dtype.categories if dtype.categories is not None else self.categories
616623
)

0 commit comments

Comments
 (0)