Skip to content
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

check ExtensionType in is_datetime64_any_dtype for array-likes #57060

Merged
merged 10 commits into from
Feb 2, 2024
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ Bug fixes
~~~~~~~~~
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
-

Categorical
^^^^^^^^^^^
Expand Down Expand Up @@ -207,7 +206,7 @@ Sparse

ExtensionArray
^^^^^^^^^^^^^^
-
- Fixed bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes and ``True`` for date types (:issue:`57055`)
-

Styler
Expand Down
15 changes: 13 additions & 2 deletions pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from __future__ import annotations

import datetime
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -881,7 +882,9 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:
"""
if isinstance(arr_or_dtype, (np.dtype, ExtensionDtype)):
# GH#33400 fastpath for dtype object
return arr_or_dtype.kind == "M"
if isinstance(arr_or_dtype, np.dtype):
return arr_or_dtype.kind == "M"
return arr_or_dtype.kind == "M" and arr_or_dtype.type != datetime.date
jmoralez marked this conversation as resolved.
Show resolved Hide resolved

if arr_or_dtype is None:
return False
Expand All @@ -890,7 +893,15 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:
tipo = _get_dtype(arr_or_dtype)
except TypeError:
return False
return lib.is_np_dtype(tipo, "M") or isinstance(tipo, DatetimeTZDtype)
return (
lib.is_np_dtype(tipo, "M")
or isinstance(tipo, DatetimeTZDtype)
or (
isinstance(tipo, ExtensionDtype)
and tipo.kind == "M"
and tipo.type != datetime.date
)
)


def is_datetime64_ns_dtype(arr_or_dtype) -> bool:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from pandas.api.extensions import no_default
from pandas.api.types import (
is_bool_dtype,
is_datetime64_any_dtype,
is_float_dtype,
is_integer_dtype,
is_numeric_dtype,
Expand Down Expand Up @@ -1531,6 +1532,14 @@ def test_is_unsigned_integer_dtype(data):
assert not is_unsigned_integer_dtype(data)


def test_is_datetime64_any_dtype(data):
pa_type = data.dtype.pyarrow_dtype
if pa.types.is_timestamp(pa_type):
assert is_datetime64_any_dtype(data)
else:
assert not is_datetime64_any_dtype(data)


def test_is_float_dtype(data):
pa_type = data.dtype.pyarrow_dtype
if pa.types.is_floating(pa_type):
Expand Down
Loading