Skip to content

Commit 1782097

Browse files
committed
asv & some typing
1 parent 96d7a95 commit 1782097

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

asv_bench/benchmarks/timestamp.py

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from .pandas_vb_common import *
22
from pandas import to_timedelta, Timestamp
3+
import pytz
4+
import datetime
35

46

57
class TimestampProperties(object):
@@ -58,3 +60,24 @@ def time_is_leap_year(self):
5860

5961
def time_microsecond(self):
6062
self.ts.microsecond
63+
64+
65+
class TimestampOps(object):
66+
goal_time = 0.2
67+
68+
def setup(self):
69+
self.ts = Timestamp('2017-08-25 08:16:14')
70+
self.ts_tz = Timestamp('2017-08-25 08:16:14', tz='US/Eastern')
71+
72+
dt = datetime.datetime(2016, 3, 27, 1)
73+
self.tzinfo = pytz.timezone('CET').localize(dt, is_dst=False).tzinfo
74+
self.ts2 = Timestamp(dt)
75+
76+
def time_replace_tz(self):
77+
self.ts.replace(tzinfo=pytz.timezone('US/Eastern'))
78+
79+
def time_replace_across_dst(self):
80+
self.ts2.replace(tzinfo=self.tzinfo)
81+
82+
def time_replace_None(self):
83+
self.ts_tz.replace(tzinfo=None)

pandas/_libs/tslib.pyx

+21-15
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def ints_to_pydatetime(ndarray[int64_t] arr, tz=None, freq=None, box=False):
144144

145145
cdef:
146146
Py_ssize_t i, n = len(arr)
147+
ndarray[int64_t] trans, deltas
147148
pandas_datetimestruct dts
148149
object dt
149150
int64_t value
@@ -433,8 +434,9 @@ class Timestamp(_Timestamp):
433434

434435
def _round(self, freq, rounder):
435436

436-
cdef int64_t unit
437-
cdef object result, value
437+
cdef:
438+
int64_t unit, r, value, buff = 1000000
439+
object result
438440

439441
from pandas.tseries.frequencies import to_offset
440442
unit = to_offset(freq).nanos
@@ -445,16 +447,15 @@ class Timestamp(_Timestamp):
445447
if unit < 1000 and unit % 1000 != 0:
446448
# for nano rounding, work with the last 6 digits separately
447449
# due to float precision
448-
buff = 1000000
449-
result = (buff * (value // buff) + unit *
450-
(rounder((value % buff) / float(unit))).astype('i8'))
450+
r = (buff * (value // buff) + unit *
451+
(rounder((value % buff) / float(unit))).astype('i8'))
451452
elif unit >= 1000 and unit % 1000 != 0:
452453
msg = 'Precision will be lost using frequency: {}'
453454
warnings.warn(msg.format(freq))
454-
result = (unit * rounder(value / float(unit)).astype('i8'))
455+
r = (unit * rounder(value / float(unit)).astype('i8'))
455456
else:
456-
result = (unit * rounder(value / float(unit)).astype('i8'))
457-
result = Timestamp(result, unit='ns')
457+
r = (unit * rounder(value / float(unit)).astype('i8'))
458+
result = Timestamp(r, unit='ns')
458459
if self.tz is not None:
459460
result = result.tz_localize(self.tz)
460461
return result
@@ -700,15 +701,15 @@ class Timestamp(_Timestamp):
700701
cdef:
701702
pandas_datetimestruct dts
702703
int64_t value, value_tz, offset
703-
object _tzinfo, result, k, v, ts_input
704+
object _tzinfo, result, k, v
705+
datetime ts_input
704706

705707
# set to naive if needed
706708
_tzinfo = self.tzinfo
707709
value = self.value
708710
if _tzinfo is not None:
709711
value_tz = tz_convert_single(value, _tzinfo, 'UTC')
710-
offset = value - value_tz
711-
value += offset
712+
value += value - value_tz
712713

713714
# setup components
714715
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts)
@@ -1191,7 +1192,7 @@ cdef class _Timestamp(datetime):
11911192
return np.datetime64(self.value, 'ns')
11921193

11931194
def __add__(self, other):
1194-
cdef int64_t other_int
1195+
cdef int64_t other_int, nanos
11951196

11961197
if is_timedelta64_object(other):
11971198
other_int = other.astype('timedelta64[ns]').view('i8')
@@ -1641,6 +1642,10 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
16411642
"""
16421643
Take a TSObject in UTC and localizes to timezone tz.
16431644
"""
1645+
cdef:
1646+
ndarray[int64_t] trans, deltas
1647+
Py_ssize_t delta, posn
1648+
16441649
if _is_utc(tz):
16451650
obj.tzinfo = tz
16461651
elif _is_tzlocal(tz):
@@ -1692,7 +1697,7 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
16921697
obj.tzinfo = tz
16931698

16941699

1695-
def _localize_pydatetime(object dt, object tz):
1700+
cpdef inline object _localize_pydatetime(object dt, object tz):
16961701
"""
16971702
Take a datetime/Timestamp in UTC and localizes to timezone tz.
16981703
"""
@@ -3929,7 +3934,7 @@ for _maybe_method_name in dir(NaTType):
39293934
# Conversion routines
39303935

39313936

3932-
def _delta_to_nanoseconds(delta):
3937+
cpdef int64_t _delta_to_nanoseconds(delta):
39333938
if isinstance(delta, np.ndarray):
39343939
return delta.astype('m8[ns]').astype('int64')
39353940
if hasattr(delta, 'nanos'):
@@ -4174,7 +4179,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
41744179
return result
41754180

41764181

4177-
def tz_convert_single(int64_t val, object tz1, object tz2):
4182+
cpdef int64_t tz_convert_single(int64_t val, object tz1, object tz2):
41784183
"""
41794184
Convert the val (in i8) from timezone1 to timezone2
41804185
@@ -5178,6 +5183,7 @@ cdef inline int64_t _normalized_stamp(pandas_datetimestruct *dts) nogil:
51785183
def dates_normalized(ndarray[int64_t] stamps, tz=None):
51795184
cdef:
51805185
Py_ssize_t i, n = len(stamps)
5186+
ndarray[int64_t] trans, deltas
51815187
pandas_datetimestruct dts
51825188

51835189
if tz is None or _is_utc(tz):

0 commit comments

Comments
 (0)