Skip to content

Commit 08a0cb0

Browse files
authored
PERF: use fast-paths for dtype checks (#34118)
1 parent 9080d30 commit 08a0cb0

File tree

4 files changed

+38
-26
lines changed

4 files changed

+38
-26
lines changed

pandas/core/arrays/interval.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,16 @@ class IntervalArray(IntervalMixin, ExtensionArray):
149149
can_hold_na = True
150150
_na_value = _fill_value = np.nan
151151

152-
def __new__(cls, data, closed=None, dtype=None, copy=False, verify_integrity=True):
152+
def __new__(
153+
cls,
154+
data,
155+
closed=None,
156+
dtype=None,
157+
copy: bool = False,
158+
verify_integrity: bool = True,
159+
):
153160

154-
if isinstance(data, ABCSeries) and is_interval_dtype(data):
161+
if isinstance(data, ABCSeries) and is_interval_dtype(data.dtype):
155162
data = data._values
156163

157164
if isinstance(data, (cls, ABCIntervalIndex)):
@@ -569,8 +576,8 @@ def __eq__(self, other):
569576

570577
# determine the dtype of the elements we want to compare
571578
if isinstance(other, Interval):
572-
other_dtype = "interval"
573-
elif not is_categorical_dtype(other):
579+
other_dtype = pandas_dtype("interval")
580+
elif not is_categorical_dtype(other.dtype):
574581
other_dtype = other.dtype
575582
else:
576583
# for categorical defer to categories for dtype
@@ -674,7 +681,8 @@ def astype(self, dtype, copy=True):
674681
array : ExtensionArray or ndarray
675682
ExtensionArray or NumPy ndarray with 'dtype' for its dtype.
676683
"""
677-
dtype = pandas_dtype(dtype)
684+
if dtype is not None:
685+
dtype = pandas_dtype(dtype)
678686
if is_interval_dtype(dtype):
679687
if dtype == self.dtype:
680688
return self.copy() if copy else self

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def __reduce__(self):
410410
def astype(self, dtype, copy=True):
411411
with rewrite_exception("IntervalArray", type(self).__name__):
412412
new_values = self._values.astype(dtype, copy=copy)
413-
if is_interval_dtype(new_values):
413+
if is_interval_dtype(new_values.dtype):
414414
return self._shallow_copy(new_values)
415415
return Index.astype(self, dtype, copy=copy)
416416

pandas/core/reshape/merge.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1079,10 +1079,10 @@ def _maybe_coerce_merge_keys(self):
10791079
if (len(lk) and not len(rk)) or (not len(lk) and len(rk)):
10801080
continue
10811081

1082-
lk_is_cat = is_categorical_dtype(lk)
1083-
rk_is_cat = is_categorical_dtype(rk)
1084-
lk_is_object = is_object_dtype(lk)
1085-
rk_is_object = is_object_dtype(rk)
1082+
lk_is_cat = is_categorical_dtype(lk.dtype)
1083+
rk_is_cat = is_categorical_dtype(rk.dtype)
1084+
lk_is_object = is_object_dtype(lk.dtype)
1085+
rk_is_object = is_object_dtype(rk.dtype)
10861086

10871087
# if either left or right is a categorical
10881088
# then the must match exactly in categories & ordered
@@ -1105,12 +1105,12 @@ def _maybe_coerce_merge_keys(self):
11051105
# kinds to proceed, eg. int64 and int8, int and float
11061106
# further if we are object, but we infer to
11071107
# the same, then proceed
1108-
if is_numeric_dtype(lk) and is_numeric_dtype(rk):
1108+
if is_numeric_dtype(lk.dtype) and is_numeric_dtype(rk.dtype):
11091109
if lk.dtype.kind == rk.dtype.kind:
11101110
continue
11111111

11121112
# check whether ints and floats
1113-
elif is_integer_dtype(rk) and is_float_dtype(lk):
1113+
elif is_integer_dtype(rk.dtype) and is_float_dtype(lk.dtype):
11141114
if not (lk == lk.astype(rk.dtype))[~np.isnan(lk)].all():
11151115
warnings.warn(
11161116
"You are merging on int and float "
@@ -1120,7 +1120,7 @@ def _maybe_coerce_merge_keys(self):
11201120
)
11211121
continue
11221122

1123-
elif is_float_dtype(rk) and is_integer_dtype(lk):
1123+
elif is_float_dtype(rk.dtype) and is_integer_dtype(lk.dtype):
11241124
if not (rk == rk.astype(lk.dtype))[~np.isnan(rk)].all():
11251125
warnings.warn(
11261126
"You are merging on int and float "
@@ -1140,14 +1140,14 @@ def _maybe_coerce_merge_keys(self):
11401140
# incompatible dtypes GH 9780, GH 15800
11411141

11421142
# bool values are coerced to object
1143-
elif (lk_is_object and is_bool_dtype(rk)) or (
1144-
is_bool_dtype(lk) and rk_is_object
1143+
elif (lk_is_object and is_bool_dtype(rk.dtype)) or (
1144+
is_bool_dtype(lk.dtype) and rk_is_object
11451145
):
11461146
pass
11471147

11481148
# object values are allowed to be merged
1149-
elif (lk_is_object and is_numeric_dtype(rk)) or (
1150-
is_numeric_dtype(lk) and rk_is_object
1149+
elif (lk_is_object and is_numeric_dtype(rk.dtype)) or (
1150+
is_numeric_dtype(lk.dtype) and rk_is_object
11511151
):
11521152
inferred_left = lib.infer_dtype(lk, skipna=False)
11531153
inferred_right = lib.infer_dtype(rk, skipna=False)
@@ -1167,13 +1167,17 @@ def _maybe_coerce_merge_keys(self):
11671167
raise ValueError(msg)
11681168

11691169
# datetimelikes must match exactly
1170-
elif needs_i8_conversion(lk) and not needs_i8_conversion(rk):
1170+
elif needs_i8_conversion(lk.dtype) and not needs_i8_conversion(rk.dtype):
11711171
raise ValueError(msg)
1172-
elif not needs_i8_conversion(lk) and needs_i8_conversion(rk):
1172+
elif not needs_i8_conversion(lk.dtype) and needs_i8_conversion(rk.dtype):
11731173
raise ValueError(msg)
1174-
elif is_datetime64tz_dtype(lk) and not is_datetime64tz_dtype(rk):
1174+
elif is_datetime64tz_dtype(lk.dtype) and not is_datetime64tz_dtype(
1175+
rk.dtype
1176+
):
11751177
raise ValueError(msg)
1176-
elif not is_datetime64tz_dtype(lk) and is_datetime64tz_dtype(rk):
1178+
elif not is_datetime64tz_dtype(lk.dtype) and is_datetime64tz_dtype(
1179+
rk.dtype
1180+
):
11771181
raise ValueError(msg)
11781182

11791183
elif lk_is_object and rk_is_object:

pandas/core/reshape/tile.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,22 @@ def _coerce_to_type(x):
461461
"""
462462
dtype = None
463463

464-
if is_datetime64tz_dtype(x):
464+
if is_datetime64tz_dtype(x.dtype):
465465
dtype = x.dtype
466-
elif is_datetime64_dtype(x):
466+
elif is_datetime64_dtype(x.dtype):
467467
x = to_datetime(x)
468468
dtype = np.dtype("datetime64[ns]")
469-
elif is_timedelta64_dtype(x):
469+
elif is_timedelta64_dtype(x.dtype):
470470
x = to_timedelta(x)
471471
dtype = np.dtype("timedelta64[ns]")
472-
elif is_bool_dtype(x):
472+
elif is_bool_dtype(x.dtype):
473473
# GH 20303
474474
x = x.astype(np.int64)
475475
# To support cut and qcut for IntegerArray we convert to float dtype.
476476
# Will properly support in the future.
477477
# https://github.com/pandas-dev/pandas/pull/31290
478478
# https://github.com/pandas-dev/pandas/issues/31389
479-
elif is_extension_array_dtype(x) and is_integer_dtype(x):
479+
elif is_extension_array_dtype(x.dtype) and is_integer_dtype(x.dtype):
480480
x = x.to_numpy(dtype=np.float64, na_value=np.nan)
481481

482482
if dtype is not None:

0 commit comments

Comments
 (0)