From 482b6e71fd5e85ee1ae715835cc6eca3a7d7c209 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 19 Sep 2022 10:27:42 -0700 Subject: [PATCH] BUG: is_datetime64tz_dtype shortcut only for DatetimeTZDtype --- doc/source/whatsnew/v1.6.0.rst | 1 + pandas/core/dtypes/common.py | 5 +++-- pandas/tests/dtypes/test_common.py | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.6.0.rst b/doc/source/whatsnew/v1.6.0.rst index 405b8cc0a5ded..61546994748c7 100644 --- a/doc/source/whatsnew/v1.6.0.rst +++ b/doc/source/whatsnew/v1.6.0.rst @@ -150,6 +150,7 @@ Conversion ^^^^^^^^^^ - Bug in constructing :class:`Series` with ``int64`` dtype from a string list raising instead of casting (:issue:`44923`) - Bug in :meth:`DataFrame.eval` incorrectly raising an ``AttributeError`` when there are negative values in function call (:issue:`46471`) +- Bug where any :class:`ExtensionDtype` subclass with ``kind="M"`` would be interpreted as a timezone type (:issue:`34986`) - Strings diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index aeb9bc7a31674..f8752e1c16c38 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -382,9 +382,10 @@ def is_datetime64tz_dtype(arr_or_dtype) -> bool: >>> is_datetime64tz_dtype(s) True """ - if isinstance(arr_or_dtype, ExtensionDtype): + if isinstance(arr_or_dtype, DatetimeTZDtype): # GH#33400 fastpath for dtype object - return arr_or_dtype.kind == "M" + # GH 34986 + return True if arr_or_dtype is None: return False diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 984655c68d56b..61c2b58388cc2 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -13,6 +13,7 @@ CategoricalDtype, CategoricalDtypeType, DatetimeTZDtype, + ExtensionDtype, IntervalDtype, PeriodDtype, ) @@ -239,6 +240,18 @@ def test_is_datetime64tz_dtype(): assert com.is_datetime64tz_dtype(pd.DatetimeIndex(["2000"], tz="US/Eastern")) +def test_custom_ea_kind_M_not_datetime64tz(): + # GH 34986 + class NotTZDtype(ExtensionDtype): + @property + def kind(self) -> str: + return "M" + + not_tz_dtype = NotTZDtype() + assert not com.is_datetime64tz_dtype(not_tz_dtype) + assert not com.needs_i8_conversion(not_tz_dtype) + + def test_is_timedelta64_dtype(): assert not com.is_timedelta64_dtype(object) assert not com.is_timedelta64_dtype(None)