@@ -52,7 +52,11 @@ from pandas._libs.tslibs.conversion cimport (
5252 convert_datetime_to_tsobject,
5353 convert_to_tsobject,
5454)
55- from pandas._libs.tslibs.dtypes cimport npy_unit_to_abbrev
55+ from pandas._libs.tslibs.dtypes cimport (
56+ npy_unit_to_abbrev,
57+ periods_per_day,
58+ periods_per_second,
59+ )
5660from pandas._libs.tslibs.util cimport (
5761 is_array,
5862 is_datetime64_object,
@@ -811,11 +815,12 @@ cdef class _Timestamp(ABCTimestamp):
811815 cdef:
812816 local_val = self ._maybe_convert_value_to_local()
813817 int64_t normalized
818+ int64_t ppd = periods_per_day(self ._reso)
814819
815820 if self._reso != NPY_FR_ns:
816821 raise NotImplementedError(self._reso )
817822
818- normalized = normalize_i8_stamp(local_val)
823+ normalized = normalize_i8_stamp(local_val, ppd )
819824 return Timestamp(normalized ).tz_localize(self.tzinfo )
820825
821826 # -----------------------------------------------------------------
@@ -834,8 +839,8 @@ cdef class _Timestamp(ABCTimestamp):
834839
835840 if len (state) == 3 :
836841 # pre-non-nano pickle
842+ # TODO: no tests get here 2022-05-10
837843 reso = NPY_FR_ns
838- assert False # checking for coverage
839844 else :
840845 reso = state[4 ]
841846 self ._reso = reso
@@ -982,10 +987,10 @@ cdef class _Timestamp(ABCTimestamp):
982987 """
983988 # GH 17329
984989 # Note: Naive timestamps will not match datetime.stdlib
985- if self ._reso != NPY_FR_ns:
986- raise NotImplementedError (self ._reso)
987990
988- return round (self .value / 1e9 , 6 )
991+ denom = periods_per_second(self ._reso)
992+
993+ return round (self .value / denom, 6 )
989994
990995 cpdef datetime to_pydatetime(_Timestamp self , bint warn = True ):
991996 """
@@ -1080,9 +1085,6 @@ cdef class _Timestamp(ABCTimestamp):
10801085 """
10811086 from pandas import Period
10821087
1083- if self ._reso != NPY_FR_ns:
1084- raise NotImplementedError (self ._reso)
1085-
10861088 if self .tz is not None :
10871089 # GH#21333
10881090 warnings.warn(
@@ -2252,16 +2254,18 @@ Timestamp.resolution = Timedelta(nanoseconds=1) # GH#21336, GH#21365
22522254
22532255
22542256@ cython.cdivision (False )
2255- cdef inline int64_t normalize_i8_stamp(int64_t local_val) nogil:
2257+ cdef inline int64_t normalize_i8_stamp(int64_t local_val, int64_t ppd ) nogil:
22562258 """
22572259 Round the localized nanosecond timestamp down to the previous midnight.
22582260
22592261 Parameters
22602262 ----------
22612263 local_val : int64_t
2264+ ppd : int64_t
2265+ Periods per day in the Timestamp's resolution.
22622266
22632267 Returns
22642268 -------
22652269 int64_t
22662270 """
2267- return local_val - (local_val % ccalendar.DAY_NANOS )
2271+ return local_val - (local_val % ppd )
0 commit comments