@@ -142,6 +142,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
142142
143143 cdef:
144144 Py_ssize_t i, n = len (arr)
145+ ndarray[int64_t] trans, deltas
145146 pandas_datetimestruct dts
146147 object dt
147148 int64_t value
@@ -417,8 +418,9 @@ class Timestamp(_Timestamp):
417418
418419 def _round (self , freq , rounder ):
419420
420- cdef int64_t unit
421- cdef object result, value
421+ cdef:
422+ int64_t unit, r, value, buff = 1000000
423+ object result
422424
423425 from pandas.tseries.frequencies import to_offset
424426 unit = to_offset(freq).nanos
@@ -429,16 +431,15 @@ class Timestamp(_Timestamp):
429431 if unit < 1000 and unit % 1000 != 0 :
430432 # for nano rounding, work with the last 6 digits separately
431433 # due to float precision
432- buff = 1000000
433- result = (buff * (value // buff) + unit *
434- (rounder((value % buff) / float (unit))).astype(' i8' ))
434+ r = (buff * (value // buff) + unit *
435+ (rounder((value % buff) / float (unit))).astype(' i8' ))
435436 elif unit >= 1000 and unit % 1000 != 0 :
436437 msg = ' Precision will be lost using frequency: {}'
437438 warnings.warn(msg.format(freq))
438- result = (unit * rounder(value / float (unit)).astype(' i8' ))
439+ r = (unit * rounder(value / float (unit)).astype(' i8' ))
439440 else :
440- result = (unit * rounder(value / float (unit)).astype(' i8' ))
441- result = Timestamp(result , unit = ' ns' )
441+ r = (unit * rounder(value / float (unit)).astype(' i8' ))
442+ result = Timestamp(r , unit = ' ns' )
442443 if self .tz is not None :
443444 result = result.tz_localize(self .tz)
444445 return result
@@ -683,14 +684,16 @@ class Timestamp(_Timestamp):
683684
684685 cdef:
685686 pandas_datetimestruct dts
686- int64_t value
687+ int64_t value, value_tz, offset
687688 object _tzinfo, result, k, v
689+ datetime ts_input
688690
689691 # set to naive if needed
690692 _tzinfo = self .tzinfo
691693 value = self .value
692694 if _tzinfo is not None :
693- value = tz_convert_single(value, ' UTC' , _tzinfo)
695+ value_tz = tz_convert_single(value, _tzinfo, ' UTC' )
696+ value += value - value_tz
694697
695698 # setup components
696699 pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, & dts)
@@ -724,16 +727,14 @@ class Timestamp(_Timestamp):
724727 _tzinfo = tzinfo
725728
726729 # reconstruct & check bounds
727- value = pandas_datetimestruct_to_datetime(PANDAS_FR_ns, & dts)
730+ ts_input = datetime(dts.year, dts.month, dts.day, dts.hour, dts.min,
731+ dts.sec, dts.us, tzinfo = _tzinfo)
732+ ts = convert_to_tsobject(ts_input, _tzinfo, None , 0 , 0 )
733+ value = ts.value + (dts.ps // 1000 )
728734 if value != NPY_NAT:
729735 _check_dts_bounds(& dts)
730736
731- # set tz if needed
732- if _tzinfo is not None :
733- value = tz_convert_single(value, _tzinfo, ' UTC' )
734-
735- result = create_timestamp_from_ts(value, dts, _tzinfo, self .freq)
736- return result
737+ return create_timestamp_from_ts(value, dts, _tzinfo, self .freq)
737738
738739 def isoformat (self , sep = ' T' ):
739740 base = super (_Timestamp, self ).isoformat(sep = sep)
@@ -1175,7 +1176,7 @@ cdef class _Timestamp(datetime):
11751176 return np.datetime64(self .value, ' ns' )
11761177
11771178 def __add__ (self , other ):
1178- cdef int64_t other_int
1179+ cdef int64_t other_int, nanos
11791180
11801181 if is_timedelta64_object(other):
11811182 other_int = other.astype(' timedelta64[ns]' ).view(' i8' )
@@ -1625,6 +1626,10 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
16251626 """
16261627 Take a TSObject in UTC and localizes to timezone tz.
16271628 """
1629+ cdef:
1630+ ndarray[int64_t] trans, deltas
1631+ Py_ssize_t delta, posn
1632+
16281633 if is_utc(tz):
16291634 obj.tzinfo = tz
16301635 elif is_tzlocal(tz):
@@ -1676,7 +1681,7 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
16761681 obj.tzinfo = tz
16771682
16781683
1679- def _localize_pydatetime (object dt , object tz ):
1684+ cpdef inline object _localize_pydatetime(object dt, object tz):
16801685 """
16811686 Take a datetime/Timestamp in UTC and localizes to timezone tz.
16821687 """
@@ -3892,7 +3897,7 @@ for _maybe_method_name in dir(NaTType):
38923897# Conversion routines
38933898
38943899
3895- def _delta_to_nanoseconds (delta ):
3900+ cpdef int64_t _delta_to_nanoseconds(delta):
38963901 if isinstance (delta, np.ndarray):
38973902 return delta.astype(' m8[ns]' ).astype(' int64' )
38983903 if hasattr (delta, ' nanos' ):
@@ -4137,7 +4142,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
41374142 return result
41384143
41394144
4140- def tz_convert_single (int64_t val , object tz1 , object tz2 ):
4145+ cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
41414146 """
41424147 Convert the val (in i8) from timezone1 to timezone2
41434148
@@ -5006,6 +5011,7 @@ cdef inline int64_t _normalized_stamp(pandas_datetimestruct *dts) nogil:
50065011def dates_normalized (ndarray[int64_t] stamps , tz = None ):
50075012 cdef:
50085013 Py_ssize_t i, n = len (stamps)
5014+ ndarray[int64_t] trans, deltas
50095015 pandas_datetimestruct dts
50105016
50115017 if tz is None or is_utc(tz):
0 commit comments