@@ -52,7 +52,11 @@ from pandas._libs.tslibs.conversion cimport (
52
52
convert_datetime_to_tsobject,
53
53
convert_to_tsobject,
54
54
)
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
+ )
56
60
from pandas._libs.tslibs.util cimport (
57
61
is_array,
58
62
is_datetime64_object,
@@ -811,11 +815,12 @@ cdef class _Timestamp(ABCTimestamp):
811
815
cdef:
812
816
local_val = self ._maybe_convert_value_to_local()
813
817
int64_t normalized
818
+ int64_t ppd = periods_per_day(self ._reso)
814
819
815
820
if self._reso != NPY_FR_ns:
816
821
raise NotImplementedError(self._reso )
817
822
818
- normalized = normalize_i8_stamp(local_val)
823
+ normalized = normalize_i8_stamp(local_val, ppd )
819
824
return Timestamp(normalized ).tz_localize(self.tzinfo )
820
825
821
826
# -----------------------------------------------------------------
@@ -834,8 +839,8 @@ cdef class _Timestamp(ABCTimestamp):
834
839
835
840
if len (state) == 3 :
836
841
# pre-non-nano pickle
842
+ # TODO: no tests get here 2022-05-10
837
843
reso = NPY_FR_ns
838
- assert False # checking for coverage
839
844
else :
840
845
reso = state[4 ]
841
846
self ._reso = reso
@@ -982,10 +987,10 @@ cdef class _Timestamp(ABCTimestamp):
982
987
"""
983
988
# GH 17329
984
989
# Note: Naive timestamps will not match datetime.stdlib
985
- if self ._reso != NPY_FR_ns:
986
- raise NotImplementedError (self ._reso)
987
990
988
- return round (self .value / 1e9 , 6 )
991
+ denom = periods_per_second(self ._reso)
992
+
993
+ return round (self .value / denom, 6 )
989
994
990
995
cpdef datetime to_pydatetime(_Timestamp self , bint warn = True ):
991
996
"""
@@ -1080,9 +1085,6 @@ cdef class _Timestamp(ABCTimestamp):
1080
1085
"""
1081
1086
from pandas import Period
1082
1087
1083
- if self ._reso != NPY_FR_ns:
1084
- raise NotImplementedError (self ._reso)
1085
-
1086
1088
if self .tz is not None :
1087
1089
# GH#21333
1088
1090
warnings.warn(
@@ -2252,16 +2254,18 @@ Timestamp.resolution = Timedelta(nanoseconds=1) # GH#21336, GH#21365
2252
2254
2253
2255
2254
2256
@ 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:
2256
2258
"""
2257
2259
Round the localized nanosecond timestamp down to the previous midnight.
2258
2260
2259
2261
Parameters
2260
2262
----------
2261
2263
local_val : int64_t
2264
+ ppd : int64_t
2265
+ Periods per day in the Timestamp's resolution.
2262
2266
2263
2267
Returns
2264
2268
-------
2265
2269
int64_t
2266
2270
"""
2267
- return local_val - (local_val % ccalendar.DAY_NANOS )
2271
+ return local_val - (local_val % ppd )
0 commit comments