Skip to content

Commit 28d5b01

Browse files
authored
ENH: Timestamp.normalize support non-nano (#47316)
1 parent f042800 commit 28d5b01

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

pandas/_libs/tslibs/timestamps.pyx

+5-4
Original file line numberDiff line numberDiff line change
@@ -856,12 +856,11 @@ cdef class _Timestamp(ABCTimestamp):
856856
local_val = self._maybe_convert_value_to_local()
857857
int64_t normalized
858858
int64_t ppd = periods_per_day(self._reso)
859-
860-
if self._reso != NPY_FR_ns:
861-
raise NotImplementedError(self._reso)
859+
_Timestamp ts
862860

863861
normalized = normalize_i8_stamp(local_val, ppd)
864-
return Timestamp(normalized).tz_localize(self.tzinfo)
862+
ts = type(self)._from_value_and_reso(normalized, reso=self._reso, tz=None)
863+
return ts.tz_localize(self.tzinfo)
865864

866865
# -----------------------------------------------------------------
867866
# Pickle Methods
@@ -2035,6 +2034,8 @@ default 'raise'
20352034
NaT
20362035
"""
20372036
if self._reso != NPY_FR_ns:
2037+
if tz is None and self.tz is None:
2038+
return self
20382039
raise NotImplementedError(self._reso)
20392040

20402041
if ambiguous == 'infer':

pandas/tests/scalar/timestamp/test_timestamp.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -715,11 +715,11 @@ def test_non_nano_construction(self, dt64, ts, reso):
715715
assert ts.value == dt64.view("i8")
716716

717717
if reso == "s":
718-
assert ts._reso == 7
718+
assert ts._reso == NpyDatetimeUnit.NPY_FR_s.value
719719
elif reso == "ms":
720-
assert ts._reso == 8
720+
assert ts._reso == NpyDatetimeUnit.NPY_FR_ms.value
721721
elif reso == "us":
722-
assert ts._reso == 9
722+
assert ts._reso == NpyDatetimeUnit.NPY_FR_us.value
723723

724724
def test_non_nano_fields(self, dt64, ts):
725725
alt = Timestamp(dt64)
@@ -830,6 +830,12 @@ def test_pickle(self, ts):
830830
assert rt._reso == ts._reso
831831
assert rt == ts
832832

833+
def test_normalize(self, dt64, ts):
834+
alt = Timestamp(dt64)
835+
result = ts.normalize()
836+
assert result._reso == ts._reso
837+
assert result == alt.normalize()
838+
833839
def test_asm8(self, dt64, ts):
834840
rt = ts.asm8
835841
assert rt == dt64

0 commit comments

Comments
 (0)