Skip to content

Commit 1bb276e

Browse files
authored
Merge branch 'pandas-dev:main' into main
2 parents a3348f6 + 4f1052e commit 1bb276e

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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
)

pandas/tests/arrays/boolean/test_logical.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,20 @@ def test_eq_mismatched_type(self, other):
6060
expected = pd.array([True, True])
6161
tm.assert_extension_array_equal(result, expected)
6262

63-
def test_logical_length_mismatch_raises(self, all_logical_operators):
63+
@pytest.mark.parametrize("other", [[True, False], [True, False, True, False]])
64+
def test_logical_length_mismatch_raises(self, other, all_logical_operators):
6465
op_name = all_logical_operators
6566
a = pd.array([True, False, None], dtype="boolean")
6667
msg = "Lengths must match"
6768

6869
with pytest.raises(ValueError, match=msg):
69-
getattr(a, op_name)([True, False])
70+
getattr(a, op_name)(other)
7071

7172
with pytest.raises(ValueError, match=msg):
72-
getattr(a, op_name)(np.array([True, False]))
73+
getattr(a, op_name)(np.array(other))
7374

7475
with pytest.raises(ValueError, match=msg):
75-
getattr(a, op_name)(pd.array([True, False], dtype="boolean"))
76+
getattr(a, op_name)(pd.array(other, dtype="boolean"))
7677

7778
def test_logical_nan_raises(self, all_logical_operators):
7879
op_name = all_logical_operators

0 commit comments

Comments
 (0)