Skip to content

Commit c3e835a

Browse files
authored
Return dtype with appropriate resolution in DatetimeTZDtype.base (pandas-dev#52706)
* Return dtype with appropriate resolution in DatetimeTZDtype.base * Whatsnew entry * Ignore type error --------- Co-authored-by: Ashwin Srinath <[email protected]>
1 parent de0753e commit c3e835a

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ Timedelta
300300
Timezones
301301
^^^^^^^^^
302302
- Bug in :func:`infer_freq` that raises ``TypeError`` for ``Series`` of timezone-aware timestamps (:issue:`52456`)
303+
- Bug in :meth:`DatetimeTZDtype.base` that always returns a NumPy dtype with nanosecond resolution (:issue:`52705`)
303304
-
304305

305306
Numeric

pandas/core/dtypes/dtypes.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,6 @@ class DatetimeTZDtype(PandasExtensionDtype):
676676
type: type[Timestamp] = Timestamp
677677
kind: str_type = "M"
678678
num = 101
679-
base = np.dtype("M8[ns]") # TODO: depend on reso?
680679
_metadata = ("unit", "tz")
681680
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
682681
_cache_dtypes: dict[str_type, PandasExtensionDtype] = {}
@@ -685,6 +684,10 @@ class DatetimeTZDtype(PandasExtensionDtype):
685684
def na_value(self) -> NaTType:
686685
return NaT
687686

687+
@cache_readonly
688+
def base(self) -> DtypeObj: # type: ignore[override]
689+
return np.dtype(f"M8[{self.unit}]")
690+
688691
# error: Signature of "str" incompatible with supertype "PandasExtensionDtype"
689692
@cache_readonly
690693
def str(self) -> str: # type: ignore[override]

pandas/tests/dtypes/test_dtypes.py

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def test_construction_non_nanosecond(self):
276276
assert res._creso == NpyDatetimeUnit.NPY_FR_ms.value
277277
assert res.str == "|M8[ms]"
278278
assert str(res) == "datetime64[ms, US/Eastern]"
279+
assert res.base == np.dtype("M8[ms]")
279280

280281
def test_day_not_supported(self):
281282
msg = "DatetimeTZDtype only supports s, ms, us, ns units"

0 commit comments

Comments
 (0)