diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 8660cbc0af1b5..234d26186aab0 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -83,7 +83,6 @@ from pandas.core.dtypes.common import ( is_all_strings, - is_datetime64_any_dtype, is_dtype_equal, is_integer_dtype, is_list_like, @@ -1390,8 +1389,11 @@ def __sub__(self, other): def __rsub__(self, other): other_dtype = getattr(other, "dtype", None) + other_is_dt64 = lib.is_np_dtype(other_dtype, "M") or isinstance( + other_dtype, DatetimeTZDtype + ) - if is_datetime64_any_dtype(other_dtype) and lib.is_np_dtype(self.dtype, "m"): + if other_is_dt64 and lib.is_np_dtype(self.dtype, "m"): # ndarray[datetime64] cannot be subtracted from self, so # we need to wrap in DatetimeArray/Index and flip the operation if lib.is_scalar(other): @@ -1403,11 +1405,7 @@ def __rsub__(self, other): other = DatetimeArray(other) return other - self - elif ( - is_datetime64_any_dtype(self.dtype) - and hasattr(other, "dtype") - and not is_datetime64_any_dtype(other.dtype) - ): + elif self.dtype.kind == "M" and hasattr(other, "dtype") and not other_is_dt64: # GH#19959 datetime - datetime is well-defined as timedelta, # but any other type - datetime is not well-defined. raise TypeError( diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index a6ef01c3a956f..84743783d2f26 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -50,7 +50,6 @@ DT64NS_DTYPE, INT64_DTYPE, is_bool_dtype, - is_datetime64_any_dtype, is_dtype_equal, is_float_dtype, is_sparse, @@ -191,7 +190,9 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps): # type: ignore[misc] _typ = "datetimearray" _internal_fill_value = np.datetime64("NaT", "ns") _recognized_scalars = (datetime, np.datetime64) - _is_recognized_dtype = is_datetime64_any_dtype + _is_recognized_dtype = lambda x: lib.is_np_dtype(x, "M") or isinstance( + x, DatetimeTZDtype + ) _infer_matches = ("datetime", "datetime64", "date") @property diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 6710f092277fd..95e7135d754cb 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -55,12 +55,14 @@ from pandas.core.dtypes.common import ( ensure_object, - is_datetime64_any_dtype, is_dtype_equal, is_period_dtype, pandas_dtype, ) -from pandas.core.dtypes.dtypes import PeriodDtype +from pandas.core.dtypes.dtypes import ( + DatetimeTZDtype, + PeriodDtype, +) from pandas.core.dtypes.generic import ( ABCIndex, ABCPeriodIndex, @@ -655,7 +657,7 @@ def astype(self, dtype, copy: bool = True): if isinstance(dtype, PeriodDtype): return self.asfreq(dtype.freq) - if is_datetime64_any_dtype(dtype): + if lib.is_np_dtype(dtype, "M") or isinstance(dtype, DatetimeTZDtype): # GH#45038 match PeriodIndex behavior. tz = getattr(dtype, "tz", None) return self.to_timestamp().tz_localize(tz) diff --git a/pandas/core/arrays/sparse/array.py b/pandas/core/arrays/sparse/array.py index f78486fa7d84f..18a1fc248f7b5 100644 --- a/pandas/core/arrays/sparse/array.py +++ b/pandas/core/arrays/sparse/array.py @@ -44,7 +44,6 @@ from pandas.core.dtypes.common import ( is_array_like, is_bool_dtype, - is_datetime64_any_dtype, is_dtype_equal, is_integer, is_list_like, @@ -559,7 +558,7 @@ def __array__(self, dtype: NpDtype | None = None) -> np.ndarray: # Can NumPy represent this type? # If not, `np.result_type` will raise. We catch that # and return object. - if is_datetime64_any_dtype(self.sp_values.dtype): + if self.sp_values.dtype.kind == "M": # However, we *do* special-case the common case of # a datetime64 with pandas NaT. if fill_value is NaT: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ac4a0ba70f83d..d10b6f51e1676 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -110,7 +110,6 @@ ensure_str, is_bool, is_bool_dtype, - is_datetime64_any_dtype, is_dict_like, is_dtype_equal, is_extension_array_dtype, @@ -7774,8 +7773,8 @@ def interpolate( methods = {"index", "values", "nearest", "time"} is_numeric_or_datetime = ( is_numeric_dtype(index.dtype) - or is_datetime64_any_dtype(index.dtype) - or lib.is_np_dtype(index.dtype, "m") + or isinstance(index.dtype, DatetimeTZDtype) + or lib.is_np_dtype(index.dtype, "mM") ) if method not in methods and not is_numeric_or_datetime: raise ValueError( diff --git a/pandas/core/methods/describe.py b/pandas/core/methods/describe.py index 4c997f2f0847c..a29e01dd173d3 100644 --- a/pandas/core/methods/describe.py +++ b/pandas/core/methods/describe.py @@ -30,10 +30,12 @@ from pandas.core.dtypes.common import ( is_bool_dtype, - is_datetime64_any_dtype, is_numeric_dtype, ) -from pandas.core.dtypes.dtypes import ExtensionDtype +from pandas.core.dtypes.dtypes import ( + DatetimeTZDtype, + ExtensionDtype, +) from pandas.core.arrays.arrow.dtype import ArrowDtype from pandas.core.arrays.floating import Float64Dtype @@ -361,7 +363,7 @@ def select_describe_func( return describe_categorical_1d elif is_numeric_dtype(data): return describe_numeric_1d - elif is_datetime64_any_dtype(data.dtype): + elif lib.is_np_dtype(data.dtype, "M") or isinstance(data.dtype, DatetimeTZDtype): return describe_timestamp_1d elif lib.is_np_dtype(data.dtype, "m"): return describe_numeric_1d diff --git a/pandas/tests/scalar/test_nat.py b/pandas/tests/scalar/test_nat.py index c13ea4eeb9e0d..0d85e37873a52 100644 --- a/pandas/tests/scalar/test_nat.py +++ b/pandas/tests/scalar/test_nat.py @@ -11,8 +11,6 @@ from pandas._libs.tslibs import iNaT from pandas.compat import is_numpy_dev -from pandas.core.dtypes.common import is_datetime64_any_dtype - from pandas import ( DatetimeIndex, DatetimeTZDtype, @@ -444,7 +442,7 @@ def test_nat_arithmetic_index(op_name, value): exp_name = "x" exp_data = [NaT] * 2 - if is_datetime64_any_dtype(value.dtype) and "plus" in op_name: + if value.dtype.kind == "M" and "plus" in op_name: expected = DatetimeIndex(exp_data, tz=value.tz, name=exp_name) else: expected = TimedeltaIndex(exp_data, name=exp_name)