Skip to content

Commit 38271a4

Browse files
authored
PERF: avoid is_datetime64_any_dtype (#52651)
* DEPR: is_datetime64_any_dtype * GH ref * Revert deprecation * revert accidental * merge mixups
1 parent bf698e2 commit 38271a4

File tree

7 files changed

+22
-23
lines changed

7 files changed

+22
-23
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 5 additions & 7 deletions
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_datetime64_any_dtype,
8786
is_dtype_equal,
8887
is_integer_dtype,
8988
is_list_like,
@@ -1390,8 +1389,11 @@ def __sub__(self, other):
13901389

13911390
def __rsub__(self, other):
13921391
other_dtype = getattr(other, "dtype", None)
1392+
other_is_dt64 = lib.is_np_dtype(other_dtype, "M") or isinstance(
1393+
other_dtype, DatetimeTZDtype
1394+
)
13931395

1394-
if is_datetime64_any_dtype(other_dtype) and lib.is_np_dtype(self.dtype, "m"):
1396+
if other_is_dt64 and lib.is_np_dtype(self.dtype, "m"):
13951397
# ndarray[datetime64] cannot be subtracted from self, so
13961398
# we need to wrap in DatetimeArray/Index and flip the operation
13971399
if lib.is_scalar(other):
@@ -1403,11 +1405,7 @@ def __rsub__(self, other):
14031405

14041406
other = DatetimeArray(other)
14051407
return other - self
1406-
elif (
1407-
is_datetime64_any_dtype(self.dtype)
1408-
and hasattr(other, "dtype")
1409-
and not is_datetime64_any_dtype(other.dtype)
1410-
):
1408+
elif self.dtype.kind == "M" and hasattr(other, "dtype") and not other_is_dt64:
14111409
# GH#19959 datetime - datetime is well-defined as timedelta,
14121410
# but any other type - datetime is not well-defined.
14131411
raise TypeError(

pandas/core/arrays/datetimes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
DT64NS_DTYPE,
5151
INT64_DTYPE,
5252
is_bool_dtype,
53-
is_datetime64_any_dtype,
5453
is_dtype_equal,
5554
is_float_dtype,
5655
is_sparse,
@@ -191,7 +190,9 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): # type: ignore[misc]
191190
_typ = "datetimearray"
192191
_internal_fill_value = np.datetime64("NaT", "ns")
193192
_recognized_scalars = (datetime, np.datetime64)
194-
_is_recognized_dtype = is_datetime64_any_dtype
193+
_is_recognized_dtype = lambda x: lib.is_np_dtype(x, "M") or isinstance(
194+
x, DatetimeTZDtype
195+
)
195196
_infer_matches = ("datetime", "datetime64", "date")
196197

197198
@property

pandas/core/arrays/period.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@
5555

5656
from pandas.core.dtypes.common import (
5757
ensure_object,
58-
is_datetime64_any_dtype,
5958
is_dtype_equal,
6059
is_period_dtype,
6160
pandas_dtype,
6261
)
63-
from pandas.core.dtypes.dtypes import PeriodDtype
62+
from pandas.core.dtypes.dtypes import (
63+
DatetimeTZDtype,
64+
PeriodDtype,
65+
)
6466
from pandas.core.dtypes.generic import (
6567
ABCIndex,
6668
ABCPeriodIndex,
@@ -655,7 +657,7 @@ def astype(self, dtype, copy: bool = True):
655657
if isinstance(dtype, PeriodDtype):
656658
return self.asfreq(dtype.freq)
657659

658-
if is_datetime64_any_dtype(dtype):
660+
if lib.is_np_dtype(dtype, "M") or isinstance(dtype, DatetimeTZDtype):
659661
# GH#45038 match PeriodIndex behavior.
660662
tz = getattr(dtype, "tz", None)
661663
return self.to_timestamp().tz_localize(tz)

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 2 deletions
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_datetime64_any_dtype,
4847
is_dtype_equal,
4948
is_integer,
5049
is_list_like,
@@ -559,7 +558,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
559558
# Can NumPy represent this type?
560559
# If not, `np.result_type` will raise. We catch that
561560
# and return object.
562-
if is_datetime64_any_dtype(self.sp_values.dtype):
561+
if self.sp_values.dtype.kind == "M":
563562
# However, we *do* special-case the common case of
564563
# a datetime64 with pandas NaT.
565564
if fill_value is NaT:

pandas/core/generic.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
ensure_str,
112112
is_bool,
113113
is_bool_dtype,
114-
is_datetime64_any_dtype,
115114
is_dict_like,
116115
is_dtype_equal,
117116
is_extension_array_dtype,
@@ -7776,8 +7775,8 @@ def interpolate(
77767775
methods = {"index", "values", "nearest", "time"}
77777776
is_numeric_or_datetime = (
77787777
is_numeric_dtype(index.dtype)
7779-
or is_datetime64_any_dtype(index.dtype)
7780-
or lib.is_np_dtype(index.dtype, "m")
7778+
or isinstance(index.dtype, DatetimeTZDtype)
7779+
or lib.is_np_dtype(index.dtype, "mM")
77817780
)
77827781
if method not in methods and not is_numeric_or_datetime:
77837782
raise ValueError(

pandas/core/methods/describe.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030

3131
from pandas.core.dtypes.common import (
3232
is_bool_dtype,
33-
is_datetime64_any_dtype,
3433
is_numeric_dtype,
3534
)
36-
from pandas.core.dtypes.dtypes import ExtensionDtype
35+
from pandas.core.dtypes.dtypes import (
36+
DatetimeTZDtype,
37+
ExtensionDtype,
38+
)
3739

3840
from pandas.core.arrays.arrow.dtype import ArrowDtype
3941
from pandas.core.arrays.floating import Float64Dtype
@@ -361,7 +363,7 @@ def select_describe_func(
361363
return describe_categorical_1d
362364
elif is_numeric_dtype(data):
363365
return describe_numeric_1d
364-
elif is_datetime64_any_dtype(data.dtype):
366+
elif lib.is_np_dtype(data.dtype, "M") or isinstance(data.dtype, DatetimeTZDtype):
365367
return describe_timestamp_1d
366368
elif lib.is_np_dtype(data.dtype, "m"):
367369
return describe_numeric_1d

pandas/tests/scalar/test_nat.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from pandas._libs.tslibs import iNaT
1212
from pandas.compat import is_numpy_dev
1313

14-
from pandas.core.dtypes.common import is_datetime64_any_dtype
15-
1614
from pandas import (
1715
DatetimeIndex,
1816
DatetimeTZDtype,
@@ -444,7 +442,7 @@ def test_nat_arithmetic_index(op_name, value):
444442
exp_name = "x"
445443
exp_data = [NaT] * 2
446444

447-
if is_datetime64_any_dtype(value.dtype) and "plus" in op_name:
445+
if value.dtype.kind == "M" and "plus" in op_name:
448446
expected = DatetimeIndex(exp_data, tz=value.tz, name=exp_name)
449447
else:
450448
expected = TimedeltaIndex(exp_data, name=exp_name)

0 commit comments

Comments
 (0)