Skip to content

Commit d8f883d

Browse files
jbrockmendeltopper-123
authored andcommitted
PERF: avoid is_dtype_equal (pandas-dev#52424)
* PERF: avoid is_dtype_equal * PERF: avoid is_dtype_equal * post-merge fixup
1 parent a0d641a commit d8f883d

31 files changed

+67
-115
lines changed

pandas/core/arrays/_mixins.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
validate_insert_loc,
3737
)
3838

39-
from pandas.core.dtypes.common import (
40-
is_dtype_equal,
41-
pandas_dtype,
42-
)
39+
from pandas.core.dtypes.common import pandas_dtype
4340
from pandas.core.dtypes.dtypes import (
4441
DatetimeTZDtype,
4542
ExtensionDtype,
@@ -172,7 +169,7 @@ def take(
172169
def equals(self, other) -> bool:
173170
if type(self) is not type(other):
174171
return False
175-
if not is_dtype_equal(self.dtype, other.dtype):
172+
if self.dtype != other.dtype:
176173
return False
177174
return bool(array_equivalent(self._ndarray, other._ndarray, dtype_equal=True))
178175

pandas/core/arrays/base.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
from pandas.core.dtypes.cast import maybe_cast_to_extension_array
4242
from pandas.core.dtypes.common import (
43-
is_dtype_equal,
4443
is_list_like,
4544
is_scalar,
4645
pandas_dtype,
@@ -570,7 +569,7 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
570569
"""
571570

572571
dtype = pandas_dtype(dtype)
573-
if is_dtype_equal(dtype, self.dtype):
572+
if dtype == self.dtype:
574573
if not copy:
575574
return self
576575
else:
@@ -958,7 +957,7 @@ def equals(self, other: object) -> bool:
958957
if type(self) != type(other):
959958
return False
960959
other = cast(ExtensionArray, other)
961-
if not is_dtype_equal(self.dtype, other.dtype):
960+
if self.dtype != other.dtype:
962961
return False
963962
elif len(self) != len(other):
964963
return False

pandas/core/arrays/categorical.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
is_any_real_numeric_dtype,
4040
is_bool_dtype,
4141
is_dict_like,
42-
is_dtype_equal,
4342
is_hashable,
4443
is_integer_dtype,
4544
is_list_like,
@@ -1394,7 +1393,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
13941393
categorical.categories.dtype.
13951394
"""
13961395
ret = take_nd(self.categories._values, self._codes)
1397-
if dtype and not is_dtype_equal(dtype, self.categories.dtype):
1396+
if dtype and np.dtype(dtype) != self.categories.dtype:
13981397
return np.asarray(ret, dtype)
13991398
# When we're a Categorical[ExtensionArray], like Interval,
14001399
# we need to ensure __array__ gets all the way to an
@@ -2018,7 +2017,7 @@ def _validate_listlike(self, value):
20182017

20192018
# require identical categories set
20202019
if isinstance(value, Categorical):
2021-
if not is_dtype_equal(self.dtype, value.dtype):
2020+
if self.dtype != value.dtype:
20222021
raise TypeError(
20232022
"Cannot set a Categorical with another, "
20242023
"without identical categories"

pandas/core/arrays/datetimelike.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383

8484
from pandas.core.dtypes.common import (
8585
is_all_strings,
86-
is_dtype_equal,
8786
is_integer_dtype,
8887
is_list_like,
8988
is_object_dtype,
@@ -664,7 +663,7 @@ def _validate_listlike(self, value, allow_object: bool = False):
664663

665664
if isinstance(value.dtype, CategoricalDtype):
666665
# e.g. we have a Categorical holding self.dtype
667-
if is_dtype_equal(value.categories.dtype, self.dtype):
666+
if value.categories.dtype == self.dtype:
668667
# TODO: do we need equal dtype or just comparable?
669668
value = value._internal_get_values()
670669
value = extract_array(value, extract_numpy=True)
@@ -1859,7 +1858,7 @@ def __init__(
18591858

18601859
if dtype is not None:
18611860
dtype = pandas_dtype(dtype)
1862-
if not is_dtype_equal(dtype, values.dtype):
1861+
if dtype != values.dtype:
18631862
# TODO: we only have tests for this for DTA, not TDA (2022-07-01)
18641863
raise TypeError(
18651864
f"dtype={dtype} does not match data dtype {values.dtype}"

pandas/core/arrays/datetimes.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
DT64NS_DTYPE,
5151
INT64_DTYPE,
5252
is_bool_dtype,
53-
is_dtype_equal,
5453
is_float_dtype,
5554
is_string_dtype,
5655
pandas_dtype,
@@ -642,7 +641,7 @@ def astype(self, dtype, copy: bool = True):
642641
# DatetimeLikeArrayMixin Super handles the rest.
643642
dtype = pandas_dtype(dtype)
644643

645-
if is_dtype_equal(dtype, self.dtype):
644+
if dtype == self.dtype:
646645
if copy:
647646
return self.copy()
648647
return self
@@ -2316,7 +2315,7 @@ def _validate_dt64_dtype(dtype):
23162315
"""
23172316
if dtype is not None:
23182317
dtype = pandas_dtype(dtype)
2319-
if is_dtype_equal(dtype, np.dtype("M8")):
2318+
if dtype == np.dtype("M8"):
23202319
# no precision, disallowed GH#24806
23212320
msg = (
23222321
"Passing in 'datetime64' dtype with no precision is not allowed. "

pandas/core/arrays/interval.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
maybe_upcast_numeric_to_64bit,
5151
)
5252
from pandas.core.dtypes.common import (
53-
is_dtype_equal,
5453
is_float_dtype,
5554
is_integer_dtype,
5655
is_list_like,
@@ -1684,7 +1683,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
16841683
# not comparable -> no overlap
16851684
return np.zeros(self.shape, dtype=bool)
16861685

1687-
if is_dtype_equal(self.dtype, values.dtype):
1686+
if self.dtype == values.dtype:
16881687
# GH#38353 instead of casting to object, operating on a
16891688
# complex128 ndarray is much more performant.
16901689
left = self._combined.view("complex128")

pandas/core/arrays/masked.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
from pandas.core.dtypes.base import ExtensionDtype
4242
from pandas.core.dtypes.common import (
4343
is_bool,
44-
is_dtype_equal,
4544
is_integer_dtype,
4645
is_list_like,
4746
is_scalar,
@@ -459,7 +458,7 @@ def astype(self, dtype: AstypeArg, copy: bool = ...) -> ArrayLike:
459458
def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike:
460459
dtype = pandas_dtype(dtype)
461460

462-
if is_dtype_equal(dtype, self.dtype):
461+
if dtype == self.dtype:
463462
if copy:
464463
return self.copy()
465464
return self

pandas/core/arrays/numpy_.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
from pandas.core.dtypes.astype import astype_array
1515
from pandas.core.dtypes.cast import construct_1d_object_array_from_listlike
16-
from pandas.core.dtypes.common import (
17-
is_dtype_equal,
18-
pandas_dtype,
19-
)
16+
from pandas.core.dtypes.common import pandas_dtype
2017
from pandas.core.dtypes.dtypes import PandasDtype
2118
from pandas.core.dtypes.missing import isna
2219

@@ -199,7 +196,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
199196
def astype(self, dtype, copy: bool = True):
200197
dtype = pandas_dtype(dtype)
201198

202-
if is_dtype_equal(dtype, self.dtype):
199+
if dtype == self.dtype:
203200
if copy:
204201
return self.copy()
205202
return self

pandas/core/arrays/period.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555

5656
from pandas.core.dtypes.common import (
5757
ensure_object,
58-
is_dtype_equal,
5958
pandas_dtype,
6059
)
6160
from pandas.core.dtypes.dtypes import (
@@ -650,7 +649,7 @@ def astype(self, dtype, copy: bool = True):
650649
# We handle Period[T] -> Period[U]
651650
# Our parent handles everything else.
652651
dtype = pandas_dtype(dtype)
653-
if is_dtype_equal(dtype, self._dtype):
652+
if dtype == self._dtype:
654653
if not copy:
655654
return self
656655
else:

pandas/core/arrays/sparse/array.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
from pandas.core.dtypes.common import (
4545
is_array_like,
4646
is_bool_dtype,
47-
is_dtype_equal,
4847
is_integer,
4948
is_list_like,
5049
is_object_dtype,
@@ -180,7 +179,7 @@ def _sparse_array_op(
180179
ltype = left.dtype.subtype
181180
rtype = right.dtype.subtype
182181

183-
if not is_dtype_equal(ltype, rtype):
182+
if ltype != rtype:
184183
subtype = find_common_type([ltype, rtype])
185184
ltype = SparseDtype(subtype, left.fill_value)
186185
rtype = SparseDtype(subtype, right.fill_value)
@@ -1240,7 +1239,7 @@ def astype(self, dtype: AstypeArg | None = None, copy: bool = True):
12401239
IntIndex
12411240
Indices: array([2, 3], dtype=int32)
12421241
"""
1243-
if is_dtype_equal(dtype, self._dtype):
1242+
if dtype == self._dtype:
12441243
if not copy:
12451244
return self
12461245
else:

pandas/core/arrays/string_.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from pandas.core.dtypes.common import (
2727
is_array_like,
2828
is_bool_dtype,
29-
is_dtype_equal,
3029
is_integer_dtype,
3130
is_object_dtype,
3231
is_string_dtype,
@@ -444,7 +443,7 @@ def _putmask(self, mask: npt.NDArray[np.bool_], value) -> None:
444443
def astype(self, dtype, copy: bool = True):
445444
dtype = pandas_dtype(dtype)
446445

447-
if is_dtype_equal(dtype, self.dtype):
446+
if dtype == self.dtype:
448447
if copy:
449448
return self.copy()
450449
return self

pandas/core/arrays/string_arrow.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
from pandas.core.dtypes.common import (
2121
is_bool_dtype,
22-
is_dtype_equal,
2322
is_integer_dtype,
2423
is_object_dtype,
2524
is_scalar,
@@ -210,7 +209,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
210209
def astype(self, dtype, copy: bool = True):
211210
dtype = pandas_dtype(dtype)
212211

213-
if is_dtype_equal(dtype, self.dtype):
212+
if dtype == self.dtype:
214213
if copy:
215214
return self.copy()
216215
return self

pandas/core/arrays/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646

4747
from pandas.core.dtypes.common import (
4848
TD64NS_DTYPE,
49-
is_dtype_equal,
5049
is_float_dtype,
5150
is_integer_dtype,
5251
is_object_dtype,
@@ -1048,7 +1047,7 @@ def _objects_to_td64ns(data, unit=None, errors: DateTimeErrorChoices = "raise"):
10481047

10491048
def _validate_td64_dtype(dtype) -> DtypeObj:
10501049
dtype = pandas_dtype(dtype)
1051-
if is_dtype_equal(dtype, np.dtype("timedelta64")):
1050+
if dtype == np.dtype("m8"):
10521051
# no precision disallowed GH#24806
10531052
msg = (
10541053
"Passing in 'timedelta' dtype with no precision is not allowed. "

pandas/core/construction.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
maybe_promote,
4444
)
4545
from pandas.core.dtypes.common import (
46-
is_dtype_equal,
4746
is_list_like,
4847
is_object_dtype,
4948
pandas_dtype,
@@ -312,9 +311,7 @@ def array(
312311
if dtype is not None:
313312
dtype = pandas_dtype(dtype)
314313

315-
if isinstance(data, ExtensionArray) and (
316-
dtype is None or is_dtype_equal(dtype, data.dtype)
317-
):
314+
if isinstance(data, ExtensionArray) and (dtype is None or data.dtype == dtype):
318315
# e.g. TimedeltaArray[s], avoid casting to PandasArray
319316
if copy:
320317
return data.copy()

pandas/core/dtypes/astype.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from pandas.errors import IntCastingNaNError
1919

2020
from pandas.core.dtypes.common import (
21-
is_dtype_equal,
2221
is_object_dtype,
2322
is_string_dtype,
2423
pandas_dtype,
@@ -171,7 +170,7 @@ def astype_array(values: ArrayLike, dtype: DtypeObj, copy: bool = False) -> Arra
171170
-------
172171
ndarray or ExtensionArray
173172
"""
174-
if is_dtype_equal(values.dtype, dtype):
173+
if values.dtype == dtype:
175174
if copy:
176175
return values.copy()
177176
return values

pandas/core/dtypes/concat.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
common_dtype_categorical_compat,
1919
find_common_type,
2020
)
21-
from pandas.core.dtypes.common import is_dtype_equal
2221
from pandas.core.dtypes.dtypes import (
2322
CategoricalDtype,
2423
DatetimeTZDtype,
@@ -276,10 +275,7 @@ def _maybe_unwrap(x):
276275
to_union = [_maybe_unwrap(x) for x in to_union]
277276
first = to_union[0]
278277

279-
if not all(
280-
is_dtype_equal(other.categories.dtype, first.categories.dtype)
281-
for other in to_union[1:]
282-
):
278+
if not lib.dtypes_all_equal([obj.categories.dtype for obj in to_union]):
283279
raise TypeError("dtype of categories must be the same")
284280

285281
ordered = False

pandas/core/dtypes/dtypes.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1248,9 +1248,7 @@ def __eq__(self, other: Any) -> bool:
12481248
elif self.closed != other.closed:
12491249
return False
12501250
else:
1251-
from pandas.core.dtypes.common import is_dtype_equal
1252-
1253-
return is_dtype_equal(self.subtype, other.subtype)
1251+
return self.subtype == other.subtype
12541252

12551253
def __setstate__(self, state) -> None:
12561254
# for pickle compat. __get_state__ is defined in the

pandas/core/dtypes/missing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
DT64NS_DTYPE,
2626
TD64NS_DTYPE,
2727
ensure_object,
28-
is_dtype_equal,
2928
is_scalar,
3029
is_string_or_object_np_dtype,
3130
)
@@ -592,7 +591,7 @@ def array_equals(left: ArrayLike, right: ArrayLike) -> bool:
592591
"""
593592
ExtensionArray-compatible implementation of array_equivalent.
594593
"""
595-
if not is_dtype_equal(left.dtype, right.dtype):
594+
if left.dtype != right.dtype:
596595
return False
597596
elif isinstance(left, ABCExtensionArray):
598597
return left.equals(right)

pandas/core/frame.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
is_bool_dtype,
9494
is_dataclass,
9595
is_dict_like,
96-
is_dtype_equal,
9796
is_float,
9897
is_float_dtype,
9998
is_hashable,
@@ -8347,7 +8346,7 @@ def combiner(x, y):
83478346
dtypes = {
83488347
col: find_common_type([self.dtypes[col], other.dtypes[col]])
83498348
for col in self.columns.intersection(other.columns)
8350-
if not is_dtype_equal(combined.dtypes[col], self.dtypes[col])
8349+
if combined.dtypes[col] != self.dtypes[col]
83518350
}
83528351

83538352
if dtypes:

0 commit comments

Comments
 (0)