Skip to content

Commit 64eed4c

Browse files
authored
PERF: slightly faster dtype.kind checks (#52387)
1 parent d86d842 commit 64eed4c

36 files changed

+73
-74
lines changed

pandas/_libs/lib.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ cpdef ndarray[object] ensure_string_array(
747747

748748
if hasattr(arr, "to_numpy"):
749749

750-
if hasattr(arr, "dtype") and arr.dtype.kind in ["m", "M"]:
750+
if hasattr(arr, "dtype") and arr.dtype.kind in "mM":
751751
# dtype check to exclude DataFrame
752752
# GH#41409 TODO: not a great place for this
753753
out = arr.astype(str).astype(object)
@@ -2641,7 +2641,7 @@ def maybe_convert_objects(ndarray[object] objects,
26412641
dtype = dtype_if_all_nat
26422642
if cnp.PyArray_DescrCheck(dtype):
26432643
# i.e. isinstance(dtype, np.dtype)
2644-
if dtype.kind not in ["m", "M"]:
2644+
if dtype.kind not in "mM":
26452645
raise ValueError(dtype)
26462646
else:
26472647
res = np.empty((<object>objects).shape, dtype=dtype)

pandas/_libs/tslibs/np_datetime.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def compare_mismatched_resolutions(ndarray left, ndarray right, op):
419419
array([ True])
420420
"""
421421

422-
if left.dtype.kind != right.dtype.kind or left.dtype.kind not in ["m", "M"]:
422+
if left.dtype.kind != right.dtype.kind or left.dtype.kind not in "mM":
423423
raise ValueError("left and right must both be timedelta64 or both datetime64")
424424

425425
cdef:

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ def _binary_op_method_timedeltalike(op, name):
775775
item = cnp.PyArray_ToScalar(cnp.PyArray_DATA(other), other)
776776
return f(self, item)
777777

778-
elif other.dtype.kind in ["m", "M"]:
778+
elif other.dtype.kind in "mM":
779779
return op(self.to_timedelta64(), other)
780780
elif other.dtype.kind == "O":
781781
return np.array([op(self, x) for x in other])
@@ -2028,7 +2028,7 @@ class Timedelta(_Timedelta):
20282028
result[mask] = np.nan
20292029
return result
20302030

2031-
elif other.dtype.kind in ["i", "u", "f"]:
2031+
elif other.dtype.kind in "iuf":
20322032
if other.ndim == 0:
20332033
return self // other.item()
20342034
else:

pandas/_libs/tslibs/timestamps.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ cdef class _Timestamp(ABCTimestamp):
464464
raise integer_op_not_supported(self)
465465

466466
elif is_array(other):
467-
if other.dtype.kind in ["i", "u"]:
467+
if other.dtype.kind in "iu":
468468
raise integer_op_not_supported(self)
469469
if other.dtype.kind == "m":
470470
if self.tz is None:
@@ -496,7 +496,7 @@ cdef class _Timestamp(ABCTimestamp):
496496
return self + neg_other
497497

498498
elif is_array(other):
499-
if other.dtype.kind in ["i", "u"]:
499+
if other.dtype.kind in "iu":
500500
raise integer_op_not_supported(self)
501501
if other.dtype.kind == "m":
502502
if self.tz is None:

pandas/_testing/asserters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def assert_interval_array_equal(
530530
_check_isinstance(left, right, IntervalArray)
531531

532532
kwargs = {}
533-
if left._left.dtype.kind in ["m", "M"]:
533+
if left._left.dtype.kind in "mM":
534534
# We have a DatetimeArray or TimedeltaArray
535535
kwargs["check_freq"] = False
536536

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def factorize_array(
568568
uniques : ndarray
569569
"""
570570
original = values
571-
if values.dtype.kind in ["m", "M"]:
571+
if values.dtype.kind in "mM":
572572
# _get_hashtable_algo will cast dt64/td64 to i8 via _ensure_data, so we
573573
# need to do the same to na_value. We are assuming here that the passed
574574
# na_value is an appropriately-typed NaT.

pandas/core/array_algos/datetimelike_accumulations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _cum_func(
5050
result = func(y)
5151
result[mask] = iNaT
5252

53-
if values.dtype.kind in ["m", "M"]:
53+
if values.dtype.kind in "mM":
5454
return result.view(values.dtype.base)
5555
return result
5656

pandas/core/array_algos/putmask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def setitem_datetimelike_compat(values: np.ndarray, num_set: int, other):
142142
if values.dtype == object:
143143
dtype, _ = infer_dtype_from(other, pandas_dtype=True)
144144

145-
if isinstance(dtype, np.dtype) and dtype.kind in ["m", "M"]:
145+
if isinstance(dtype, np.dtype) and dtype.kind in "mM":
146146
# https://github.com/numpy/numpy/issues/12550
147147
# timedelta64 will incorrectly cast to int
148148
if not is_list_like(other):

pandas/core/array_algos/quantile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _nanpercentile(
180180
quantiles : scalar or array
181181
"""
182182

183-
if values.dtype.kind in ["m", "M"]:
183+
if values.dtype.kind in "mM":
184184
# need to cast to integer to avoid rounding errors in numpy
185185
result = _nanpercentile(
186186
values.view("i8"),

pandas/core/arrays/datetimelike.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
740740
if not hasattr(values, "dtype"):
741741
values = np.asarray(values)
742742

743-
if values.dtype.kind in ["f", "i", "u", "c"]:
743+
if values.dtype.kind in "fiuc":
744744
# TODO: de-duplicate with equals, validate_comparison_value
745745
return np.zeros(self.shape, dtype=bool)
746746

@@ -769,7 +769,7 @@ def isin(self, values) -> npt.NDArray[np.bool_]:
769769
except ValueError:
770770
return isin(self.astype(object), values)
771771

772-
if self.dtype.kind in ["m", "M"]:
772+
if self.dtype.kind in "mM":
773773
self = cast("DatetimeArray | TimedeltaArray", self)
774774
values = values.as_unit(self.unit)
775775

@@ -1205,7 +1205,7 @@ def _sub_nat(self):
12051205
# For period dtype, timedelta64 is a close-enough return dtype.
12061206
result = np.empty(self.shape, dtype=np.int64)
12071207
result.fill(iNaT)
1208-
if self.dtype.kind in ["m", "M"]:
1208+
if self.dtype.kind in "mM":
12091209
# We can retain unit in dtype
12101210
self = cast("DatetimeArray| TimedeltaArray", self)
12111211
return result.view(f"timedelta64[{self.unit}]")

0 commit comments

Comments
 (0)