Skip to content

Commit 9c76d54

Browse files
authored
check ExtensionType in is_datetime64_any_dtype for array-likes (#57060)
* check for ExtensionType in is_datetime64_any_dtype * use pre-commit * add test and move doc entry * check not date in test * fix condition * check type is not datetime.date * fix comparison * move description to ExtensionArray * return True for date types
1 parent fc05632 commit 9c76d54

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ Bug fixes
130130
~~~~~~~~~
131131
- Fixed bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
132132
- Fixed bug in :meth:`Series.diff` allowing non-integer values for the ``periods`` argument. (:issue:`56607`)
133-
-
134133

135134
Categorical
136135
^^^^^^^^^^^
@@ -219,7 +218,7 @@ Sparse
219218

220219
ExtensionArray
221220
^^^^^^^^^^^^^^
222-
-
221+
- Fixed bug in :meth:`api.types.is_datetime64_any_dtype` where a custom :class:`ExtensionDtype` would return ``False`` for array-likes (:issue:`57055`)
223222
-
224223

225224
Styler

pandas/core/dtypes/common.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,11 @@ def is_datetime64_any_dtype(arr_or_dtype) -> bool:
890890
tipo = _get_dtype(arr_or_dtype)
891891
except TypeError:
892892
return False
893-
return lib.is_np_dtype(tipo, "M") or isinstance(tipo, DatetimeTZDtype)
893+
return (
894+
lib.is_np_dtype(tipo, "M")
895+
or isinstance(tipo, DatetimeTZDtype)
896+
or (isinstance(tipo, ExtensionDtype) and tipo.kind == "M")
897+
)
894898

895899

896900
def is_datetime64_ns_dtype(arr_or_dtype) -> bool:

pandas/tests/extension/test_arrow.py

+9
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from pandas.api.extensions import no_default
5454
from pandas.api.types import (
5555
is_bool_dtype,
56+
is_datetime64_any_dtype,
5657
is_float_dtype,
5758
is_integer_dtype,
5859
is_numeric_dtype,
@@ -1531,6 +1532,14 @@ def test_is_unsigned_integer_dtype(data):
15311532
assert not is_unsigned_integer_dtype(data)
15321533

15331534

1535+
def test_is_datetime64_any_dtype(data):
1536+
pa_type = data.dtype.pyarrow_dtype
1537+
if pa.types.is_timestamp(pa_type) or pa.types.is_date(pa_type):
1538+
assert is_datetime64_any_dtype(data)
1539+
else:
1540+
assert not is_datetime64_any_dtype(data)
1541+
1542+
15341543
def test_is_float_dtype(data):
15351544
pa_type = data.dtype.pyarrow_dtype
15361545
if pa.types.is_floating(pa_type):

0 commit comments

Comments
 (0)