diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 147d337abb11a..059dd36567877 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -298,6 +298,7 @@ Timedelta Timezones ^^^^^^^^^ - Bug in :func:`infer_freq` that raises ``TypeError`` for ``Series`` of timezone-aware timestamps (:issue:`52456`) +- Bug in :meth:`DatetimeTZDtype.base` that always returns a NumPy dtype with nanosecond resolution (:issue:`52705`) - Numeric diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 65b612ce019dd..479bfdc557a07 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -676,7 +676,6 @@ class DatetimeTZDtype(PandasExtensionDtype): type: type[Timestamp] = Timestamp kind: str_type = "M" num = 101 - base = np.dtype("M8[ns]") # TODO: depend on reso? _metadata = ("unit", "tz") _match = re.compile(r"(datetime64|M8)\[(?P.+), (?P.+)\]") _cache_dtypes: dict[str_type, PandasExtensionDtype] = {} @@ -685,6 +684,10 @@ class DatetimeTZDtype(PandasExtensionDtype): def na_value(self) -> NaTType: return NaT + @cache_readonly + def base(self) -> DtypeObj: # type: ignore[override] + return np.dtype(f"M8[{self.unit}]") + # error: Signature of "str" incompatible with supertype "PandasExtensionDtype" @cache_readonly def str(self) -> str: # type: ignore[override] diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 09e100c660ddf..077cbbc72e048 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -276,6 +276,7 @@ def test_construction_non_nanosecond(self): assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value assert res.str == "|M8[ms]" assert str(res) == "datetime64[ms, US/Eastern]" + assert res.base == np.dtype("M8[ms]") def test_day_not_supported(self): msg = "DatetimeTZDtype only supports s, ms, us, ns units"