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 4 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ Deprecations
- Deprecated parameter ``convert_type`` in :meth:`Series.apply` (:issue:`52140`)
- Deprecated passing a dictionary to :meth:`.SeriesGroupBy.agg`; pass a list of aggregations instead (:issue:`50684`)
- Deprecated the "fastpath" keyword in :class:`Categorical` constructor, use :meth:`Categorical.from_codes` instead (:issue:`20110`)
- Deprecated the behavior of :func:`is_bool_dtype` returning ``True`` for object-dtype :class:`Index` of bool objects (:issue:`52680`)
- Deprecated the methods :meth:`Series.bool` and :meth:`DataFrame.bool` (:issue:`51749`)
- Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`)
- Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`)
Expand Down
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_object_dtype,
Expand Down Expand Up @@ -192,7 +191,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
13 changes: 12 additions & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,18 @@ def is_bool_dtype(arr_or_dtype) -> bool:

if isinstance(arr_or_dtype, ABCIndex):
# Allow Index[object] that is all-bools or Index["boolean"]
return arr_or_dtype.inferred_type == "boolean"
if arr_or_dtype.inferred_type == "boolean":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this looks unrelated. Did a merge conflict go wrong in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, will revert

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated + green

if not is_bool_dtype(arr_or_dtype.dtype):
# GH#52680
warnings.warn(
"The behavior of is_bool_dtype with an object-dtype Index "
"of bool objects is deprecated. In a future version, "
"this will return False. Cast the Index to a bool dtype instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return True
return False
elif isinstance(dtype, ExtensionDtype):
return getattr(dtype, "_is_boolean", False)

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 @@ -7754,8 +7753,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
4 changes: 2 additions & 2 deletions pandas/core/methods/describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
from pandas.core.dtypes.common import (
is_bool_dtype,
is_complex_dtype,
is_datetime64_any_dtype,
is_extension_array_dtype,
is_numeric_dtype,
)
from pandas.core.dtypes.dtypes import DatetimeTZDtype

from pandas.core.arrays.arrow.dtype import ArrowDtype
from pandas.core.arrays.floating import Float64Dtype
Expand Down Expand Up @@ -361,7 +361,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