-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: non-nano Timestamp.timestamp, to_period #46990
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -348,6 +348,19 @@ cdef int64_t periods_per_day(NPY_DATETIMEUNIT reso=NPY_DATETIMEUNIT.NPY_FR_ns) e | |
return day_units | ||
|
||
|
||
cdef int64_t periods_per_second(NPY_DATETIMEUNIT reso) except? -1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can't be defined with |
||
if reso == NPY_DATETIMEUNIT.NPY_FR_ns: | ||
return 1_000_000_000 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_us: | ||
return 1_000_000 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_ms: | ||
return 1_000 | ||
elif reso == NPY_DATETIMEUNIT.NPY_FR_s: | ||
return 1 | ||
else: | ||
raise NotImplementedError(reso) | ||
|
||
|
||
cdef dict _reso_str_map = { | ||
Resolution.RESO_NS.value: "nanosecond", | ||
Resolution.RESO_US.value: "microsecond", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,11 @@ from pandas._libs.tslibs.conversion cimport ( | |
convert_datetime_to_tsobject, | ||
convert_to_tsobject, | ||
) | ||
from pandas._libs.tslibs.dtypes cimport npy_unit_to_abbrev | ||
from pandas._libs.tslibs.dtypes cimport ( | ||
npy_unit_to_abbrev, | ||
periods_per_day, | ||
periods_per_second, | ||
) | ||
from pandas._libs.tslibs.util cimport ( | ||
is_array, | ||
is_datetime64_object, | ||
|
@@ -811,11 +815,12 @@ cdef class _Timestamp(ABCTimestamp): | |
cdef: | ||
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) | ||
|
||
normalized = normalize_i8_stamp(local_val) | ||
normalized = normalize_i8_stamp(local_val, ppd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have non-nano tests for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not yet, still raises a few lines up, per commit message this is preliminary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have non-nano tests for |
||
return Timestamp(normalized).tz_localize(self.tzinfo) | ||
|
||
# ----------------------------------------------------------------- | ||
|
@@ -834,8 +839,8 @@ cdef class _Timestamp(ABCTimestamp): | |
|
||
if len(state) == 3: | ||
# pre-non-nano pickle | ||
# TODO: no tests get here 2022-05-10 | ||
reso = NPY_FR_ns | ||
assert False # checking for coverage | ||
else: | ||
reso = state[4] | ||
self._reso = reso | ||
|
@@ -982,10 +987,10 @@ cdef class _Timestamp(ABCTimestamp): | |
""" | ||
# GH 17329 | ||
# Note: Naive timestamps will not match datetime.stdlib | ||
if self._reso != NPY_FR_ns: | ||
raise NotImplementedError(self._reso) | ||
|
||
return round(self.value / 1e9, 6) | ||
denom = periods_per_second(self._reso) | ||
|
||
return round(self.value / denom, 6) | ||
|
||
cpdef datetime to_pydatetime(_Timestamp self, bint warn=True): | ||
""" | ||
|
@@ -1080,9 +1085,6 @@ cdef class _Timestamp(ABCTimestamp): | |
""" | ||
from pandas import Period | ||
|
||
if self._reso != NPY_FR_ns: | ||
raise NotImplementedError(self._reso) | ||
|
||
if self.tz is not None: | ||
# GH#21333 | ||
warnings.warn( | ||
|
@@ -2252,16 +2254,18 @@ Timestamp.resolution = Timedelta(nanoseconds=1) # GH#21336, GH#21365 | |
|
||
|
||
@cython.cdivision(False) | ||
cdef inline int64_t normalize_i8_stamp(int64_t local_val) nogil: | ||
cdef inline int64_t normalize_i8_stamp(int64_t local_val, int64_t ppd) nogil: | ||
""" | ||
Round the localized nanosecond timestamp down to the previous midnight. | ||
|
||
Parameters | ||
---------- | ||
local_val : int64_t | ||
ppd : int64_t | ||
Periods per day in the Timestamp's resolution. | ||
|
||
Returns | ||
------- | ||
int64_t | ||
""" | ||
return local_val - (local_val % ccalendar.DAY_NANOS) | ||
return local_val - (local_val % ppd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can't be defined with
periods_per_day
because of timezones?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could share for some cases, but for not for e.g. NPY_FR_h