Skip to content

PERF: avoid is_datetime64_any_dtype #52651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
DT64NS_DTYPE,
INT64_DTYPE,
is_bool_dtype,
is_datetime64_any_dtype,
is_dtype_equal,
is_float_dtype,
is_sparse,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/arrays/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/arrays/sparse/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 5 additions & 3 deletions pandas/core/methods/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/scalar/test_nat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down