Skip to content

ENH: Timestamp.normalize support non-nano #47316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,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
Expand Down Expand Up @@ -2035,6 +2034,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':
Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,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)
Expand Down Expand Up @@ -830,6 +830,12 @@ def test_pickle(self, ts):
assert rt._reso == ts._reso
assert rt == ts

def test_normalize(self, dt64, ts):
alt = Timestamp(dt64)
result = ts.normalize()
assert result._reso == ts._reso
assert result == alt.normalize()

def test_asm8(self, dt64, ts):
rt = ts.asm8
assert rt == dt64
Expand Down