From 24c00c50515e417c349d139b146aa38729331d3a Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 11 Jun 2022 20:12:36 -0700 Subject: [PATCH] ENH: Timestamp.normalize support non-nano --- pandas/_libs/tslibs/timestamps.pyx | 9 +++++---- pandas/tests/scalar/timestamp/test_timestamp.py | 13 ++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index c6bae70d04a98..4c8ef616e59bf 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -854,12 +854,11 @@ cdef class _Timestamp(ABCTimestamp): local_val = self._maybe_convert_value_to_local() int64_t normalized int64_t ppd = periods_per_day(self._reso) - - if self._reso != NPY_FR_ns: - raise NotImplementedError(self._reso) + _Timestamp ts normalized = normalize_i8_stamp(local_val, ppd) - return Timestamp(normalized).tz_localize(self.tzinfo) + ts = type(self)._from_value_and_reso(normalized, reso=self._reso, tz=None) + return ts.tz_localize(self.tzinfo) # ----------------------------------------------------------------- # Pickle Methods @@ -1997,6 +1996,8 @@ default 'raise' NaT """ if self._reso != NPY_FR_ns: + if tz is None and self.tz is None: + return self raise NotImplementedError(self._reso) if ambiguous == 'infer': diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 89e5ce2241e42..6ccdc39748c58 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -18,6 +18,7 @@ utc, ) +from pandas._libs.tslibs.dtypes import NpyDatetimeUnit from pandas._libs.tslibs.timezones import ( dateutil_gettz as gettz, get_timezone, @@ -713,11 +714,11 @@ def test_non_nano_construction(self, dt64, ts, reso): assert ts.value == dt64.view("i8") if reso == "s": - assert ts._reso == 7 + assert ts._reso == NpyDatetimeUnit.NPY_FR_s.value elif reso == "ms": - assert ts._reso == 8 + assert ts._reso == NpyDatetimeUnit.NPY_FR_ms.value elif reso == "us": - assert ts._reso == 9 + assert ts._reso == NpyDatetimeUnit.NPY_FR_us.value def test_non_nano_fields(self, dt64, ts): alt = Timestamp(dt64) @@ -850,3 +851,9 @@ def test_timestamp(self, dt64, ts): def test_to_period(self, dt64, ts): alt = Timestamp(dt64) assert ts.to_period("D") == alt.to_period("D") + + def test_normalize(self, dt64, ts): + alt = Timestamp(dt64) + result = ts.normalize() + assert result._reso == ts._reso + assert result == alt.normalize()