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 2 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 @@ -234,6 +234,7 @@ Deprecations
- Deprecated :func:`is_datetime64tz_dtype`, check ``isinstance(dtype, pd.DatetimeTZDtype)`` instead (:issue:`52607`)
- Deprecated unused "closed" and "normalize" keywords in the :class:`DatetimeIndex` constructor (:issue:`52628`)
- Deprecated unused "closed" keyword in the :class:`TimedeltaIndex` constructor (:issue:`52628`)
- Deprecated :func:`is_datetime64_any_dtype`, check for specific dtypes of interest instead (:issue:`52651`)
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to keep utility functions that are more complicated to reimplement? A single is instance check is fine, but more complicated things would cause duplication for users.

Copy link
Member Author

Choose a reason for hiding this comment

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

i guess. all i really care about is that we dont use this internally. but i do think its a footgun since pretty much anyone who uses it wrote it with only numpy/pd.DatetimeTZDtype in mind

-

.. ---------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def pytest_collection_modifyitems(items, config) -> None:
# Each entry specifies (path, message) - see the ignore_doctest_warning function
ignored_doctest_warnings = [
("is_int64_dtype", "is_int64_dtype is deprecated"),
("is_datetime64_any_dtype", "is_datetime64_any_dtype is deprecated"),
("is_interval_dtype", "is_interval_dtype is deprecated"),
("is_datetime64tz_dtype", "is_datetime64tz_dtype is deprecated"),
# Docstring divides by zero to show behavior difference
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_datetime_or_timedelta_dtype,
is_dtype_equal,
is_float_dtype,
Expand Down Expand Up @@ -1395,8 +1394,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 @@ -1408,11 +1410,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_datetime64_dtype,
is_dtype_equal,
is_float_dtype,
Expand Down Expand Up @@ -193,7 +192,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,15 +55,17 @@

from pandas.core.dtypes.common import (
ensure_object,
is_datetime64_any_dtype,
is_datetime64_dtype,
is_dtype_equal,
is_float_dtype,
is_integer_dtype,
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 @@ -658,7 +660,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
7 changes: 7 additions & 0 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,13 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:
>>> is_datetime64_any_dtype(pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]"))
True
"""
# GH#52651
warnings.warn(
"is_datetime64_any_dtype is deprecated and will be removed in a future "
"version. Check for specific dtypes of interest instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
if isinstance(arr_or_dtype, (np.dtype, ExtensionDtype)):
# GH#33400 fastpath for dtype object
return arr_or_dtype.kind == "M"
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
25 changes: 14 additions & 11 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def test_get_dtype_error_catch(func):
if (
func is com.is_int64_dtype
or func is com.is_interval_dtype
or func is com.is_datetime64_any_dtype
or func is com.is_datetime64tz_dtype
or func is com.is_categorical_dtype
):
Expand Down Expand Up @@ -471,17 +472,19 @@ def test_is_not_int64_dtype(dtype):


def test_is_datetime64_any_dtype():
assert not com.is_datetime64_any_dtype(int)
assert not com.is_datetime64_any_dtype(str)
assert not com.is_datetime64_any_dtype(np.array([1, 2]))
assert not com.is_datetime64_any_dtype(np.array(["a", "b"]))

assert com.is_datetime64_any_dtype(np.datetime64)
assert com.is_datetime64_any_dtype(np.array([], dtype=np.datetime64))
assert com.is_datetime64_any_dtype(DatetimeTZDtype("ns", "US/Eastern"))
assert com.is_datetime64_any_dtype(
pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")
)
msg = "is_datetime64_any_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert not com.is_datetime64_any_dtype(int)
assert not com.is_datetime64_any_dtype(str)
assert not com.is_datetime64_any_dtype(np.array([1, 2]))
assert not com.is_datetime64_any_dtype(np.array(["a", "b"]))

assert com.is_datetime64_any_dtype(np.datetime64)
assert com.is_datetime64_any_dtype(np.array([], dtype=np.datetime64))
assert com.is_datetime64_any_dtype(DatetimeTZDtype("ns", "US/Eastern"))
assert com.is_datetime64_any_dtype(
pd.DatetimeIndex([1, 2, 3], dtype="datetime64[ns]")
)


def test_is_datetime64_ns_dtype():
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,11 @@ def test_compat(self, dtype):
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64tz_dtype(dtype)
assert is_datetime64tz_dtype("datetime64[ns, US/Eastern]")
assert is_datetime64_any_dtype(dtype)
assert is_datetime64_any_dtype("datetime64[ns, US/Eastern]")

msg = "is_datetime64_any_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64_any_dtype(dtype)
assert is_datetime64_any_dtype("datetime64[ns, US/Eastern]")
assert is_datetime64_ns_dtype(dtype)
assert is_datetime64_ns_dtype("datetime64[ns, US/Eastern]")
assert not is_datetime64_dtype(dtype)
Expand Down Expand Up @@ -1130,6 +1133,7 @@ def test_is_dtype_no_warning(check):
if (
check is is_categorical_dtype
or check is is_interval_dtype
or check is is_datetime64_any_dtype
or check is is_datetime64tz_dtype
):
warn = FutureWarning
Expand Down
15 changes: 10 additions & 5 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,7 @@ def test_is_datetime_dtypes(self):
tsa = pd.date_range("20130101", periods=3, tz="US/Eastern")

msg = "is_datetime64tz_dtype is deprecated"
msg_any = "is_datetime64_any_dtype is deprecated"

assert is_datetime64_dtype("datetime64")
assert is_datetime64_dtype("datetime64[ns]")
Expand All @@ -1833,10 +1834,11 @@ def test_is_datetime_dtypes(self):
assert is_datetime64_ns_dtype(ts)
assert is_datetime64_ns_dtype(tsa)

assert is_datetime64_any_dtype("datetime64")
assert is_datetime64_any_dtype("datetime64[ns]")
assert is_datetime64_any_dtype(ts)
assert is_datetime64_any_dtype(tsa)
with tm.assert_produces_warning(FutureWarning, match=msg_any):
assert is_datetime64_any_dtype("datetime64")
assert is_datetime64_any_dtype("datetime64[ns]")
assert is_datetime64_any_dtype(ts)
assert is_datetime64_any_dtype(tsa)

with tm.assert_produces_warning(FutureWarning, match=msg):
assert not is_datetime64tz_dtype("datetime64")
Expand All @@ -1853,7 +1855,10 @@ def test_is_datetime_dtypes_with_tz(self, tz):
with tm.assert_produces_warning(FutureWarning, match=msg):
assert is_datetime64tz_dtype(dtype)
assert is_datetime64_ns_dtype(dtype)
assert is_datetime64_any_dtype(dtype)

msg_any = "is_datetime64_any_dtype is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg_any):
assert is_datetime64_any_dtype(dtype)

def test_is_timedelta(self):
assert is_timedelta64_dtype("timedelta64")
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