@@ -142,6 +142,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
142
142
143
143
cdef:
144
144
Py_ssize_t i, n = len (arr)
145
+ ndarray[int64_t] trans, deltas
145
146
pandas_datetimestruct dts
146
147
object dt
147
148
int64_t value
@@ -417,8 +418,9 @@ class Timestamp(_Timestamp):
417
418
418
419
def _round (self , freq , rounder ):
419
420
420
- cdef int64_t unit
421
- cdef object result, value
421
+ cdef:
422
+ int64_t unit, r, value, buff = 1000000
423
+ object result
422
424
423
425
from pandas.tseries.frequencies import to_offset
424
426
unit = to_offset(freq).nanos
@@ -429,16 +431,15 @@ class Timestamp(_Timestamp):
429
431
if unit < 1000 and unit % 1000 != 0 :
430
432
# for nano rounding, work with the last 6 digits separately
431
433
# 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' ))
435
436
elif unit >= 1000 and unit % 1000 != 0 :
436
437
msg = ' Precision will be lost using frequency: {}'
437
438
warnings.warn(msg.format(freq))
438
- result = (unit * rounder(value / float (unit)).astype(' i8' ))
439
+ r = (unit * rounder(value / float (unit)).astype(' i8' ))
439
440
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' )
442
443
if self .tz is not None :
443
444
result = result.tz_localize(self .tz)
444
445
return result
@@ -683,14 +684,16 @@ class Timestamp(_Timestamp):
683
684
684
685
cdef:
685
686
pandas_datetimestruct dts
686
- int64_t value
687
+ int64_t value, value_tz, offset
687
688
object _tzinfo, result, k, v
689
+ datetime ts_input
688
690
689
691
# set to naive if needed
690
692
_tzinfo = self .tzinfo
691
693
value = self .value
692
694
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
694
697
695
698
# setup components
696
699
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, & dts)
@@ -724,16 +727,14 @@ class Timestamp(_Timestamp):
724
727
_tzinfo = tzinfo
725
728
726
729
# 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 )
728
734
if value != NPY_NAT:
729
735
_check_dts_bounds(& dts)
730
736
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)
737
738
738
739
def isoformat (self , sep = ' T' ):
739
740
base = super (_Timestamp, self ).isoformat(sep = sep)
@@ -1175,7 +1176,7 @@ cdef class _Timestamp(datetime):
1175
1176
return np.datetime64(self .value, ' ns' )
1176
1177
1177
1178
def __add__ (self , other ):
1178
- cdef int64_t other_int
1179
+ cdef int64_t other_int, nanos
1179
1180
1180
1181
if is_timedelta64_object(other):
1181
1182
other_int = other.astype(' timedelta64[ns]' ).view(' i8' )
@@ -1625,6 +1626,10 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
1625
1626
"""
1626
1627
Take a TSObject in UTC and localizes to timezone tz.
1627
1628
"""
1629
+ cdef:
1630
+ ndarray[int64_t] trans, deltas
1631
+ Py_ssize_t delta, posn
1632
+
1628
1633
if is_utc(tz):
1629
1634
obj.tzinfo = tz
1630
1635
elif is_tzlocal(tz):
@@ -1676,7 +1681,7 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
1676
1681
obj.tzinfo = tz
1677
1682
1678
1683
1679
- def _localize_pydatetime (object dt , object tz ):
1684
+ cpdef inline object _localize_pydatetime(object dt, object tz):
1680
1685
"""
1681
1686
Take a datetime/Timestamp in UTC and localizes to timezone tz.
1682
1687
"""
@@ -3892,7 +3897,7 @@ for _maybe_method_name in dir(NaTType):
3892
3897
# Conversion routines
3893
3898
3894
3899
3895
- def _delta_to_nanoseconds (delta ):
3900
+ cpdef int64_t _delta_to_nanoseconds(delta):
3896
3901
if isinstance (delta, np.ndarray):
3897
3902
return delta.astype(' m8[ns]' ).astype(' int64' )
3898
3903
if hasattr (delta, ' nanos' ):
@@ -4137,7 +4142,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
4137
4142
return result
4138
4143
4139
4144
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):
4141
4146
"""
4142
4147
Convert the val (in i8) from timezone1 to timezone2
4143
4148
@@ -5006,6 +5011,7 @@ cdef inline int64_t _normalized_stamp(pandas_datetimestruct *dts) nogil:
5006
5011
def dates_normalized (ndarray[int64_t] stamps , tz = None ):
5007
5012
cdef:
5008
5013
Py_ssize_t i, n = len (stamps)
5014
+ ndarray[int64_t] trans, deltas
5009
5015
pandas_datetimestruct dts
5010
5016
5011
5017
if tz is None or is_utc(tz):
0 commit comments