Skip to content

Commit 45a7012

Browse files
mroeschkejreback
authored andcommitted
CLN: ASV timedelta (#19150)
1 parent 7c8c1fd commit 45a7012

File tree

1 file changed

+54
-43
lines changed

1 file changed

+54
-43
lines changed

asv_bench/benchmarks/timedelta.py

+54-43
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import datetime
22

33
import numpy as np
4-
import pandas as pd
5-
6-
from pandas import to_timedelta, Timestamp, Timedelta
4+
from pandas import Series, timedelta_range, to_timedelta, Timestamp, Timedelta
75

86

97
class TimedeltaConstructor(object):
8+
109
goal_time = 0.2
1110

1211
def time_from_int(self):
@@ -36,35 +35,44 @@ def time_from_missing(self):
3635

3736

3837
class ToTimedelta(object):
38+
3939
goal_time = 0.2
4040

4141
def setup(self):
42-
self.arr = np.random.randint(0, 1000, size=10000)
43-
self.arr2 = ['{0} days'.format(i) for i in self.arr]
44-
45-
self.arr3 = np.random.randint(0, 60, size=10000)
46-
self.arr3 = ['00:00:{0:02d}'.format(i) for i in self.arr3]
47-
48-
self.arr4 = list(self.arr2)
49-
self.arr4[-1] = 'apple'
42+
self.ints = np.random.randint(0, 60, size=10000)
43+
self.str_days = []
44+
self.str_seconds = []
45+
for i in self.ints:
46+
self.str_days.append('{0} days'.format(i))
47+
self.str_seconds.append('00:00:{0:02d}'.format(i))
5048

5149
def time_convert_int(self):
52-
to_timedelta(self.arr, unit='s')
50+
to_timedelta(self.ints, unit='s')
5351

54-
def time_convert_string(self):
55-
to_timedelta(self.arr2)
52+
def time_convert_string_days(self):
53+
to_timedelta(self.str_days)
5654

5755
def time_convert_string_seconds(self):
58-
to_timedelta(self.arr3)
56+
to_timedelta(self.str_seconds)
57+
58+
59+
class ToTimedeltaErrors(object):
5960

60-
def time_convert_coerce(self):
61-
to_timedelta(self.arr4, errors='coerce')
61+
goal_time = 0.2
62+
params = ['coerce', 'ignore']
63+
param_names = ['errors']
64+
65+
def setup(self, errors):
66+
ints = np.random.randint(0, 60, size=10000)
67+
self.arr = ['{0} days'.format(i) for i in ints]
68+
self.arr[-1] = 'apple'
6269

63-
def time_convert_ignore(self):
64-
to_timedelta(self.arr4, errors='ignore')
70+
def time_convert(self, errors):
71+
to_timedelta(self.arr, errors=errors)
6572

6673

6774
class TimedeltaOps(object):
75+
6876
goal_time = 0.2
6977

7078
def setup(self):
@@ -76,43 +84,46 @@ def time_add_td_ts(self):
7684

7785

7886
class TimedeltaProperties(object):
87+
7988
goal_time = 0.2
8089

81-
def setup(self):
82-
self.td = Timedelta(days=365, minutes=35, seconds=25, milliseconds=35)
90+
def setup_cache(self):
91+
td = Timedelta(days=365, minutes=35, seconds=25, milliseconds=35)
92+
return td
8393

84-
def time_timedelta_days(self):
85-
self.td.days
94+
def time_timedelta_days(self, td):
95+
td.days
8696

87-
def time_timedelta_seconds(self):
88-
self.td.seconds
97+
def time_timedelta_seconds(self, td):
98+
td.seconds
8999

90-
def time_timedelta_microseconds(self):
91-
self.td.microseconds
100+
def time_timedelta_microseconds(self, td):
101+
td.microseconds
92102

93-
def time_timedelta_nanoseconds(self):
94-
self.td.nanoseconds
103+
def time_timedelta_nanoseconds(self, td):
104+
td.nanoseconds
95105

96106

97107
class DatetimeAccessor(object):
108+
98109
goal_time = 0.2
99110

100-
def setup(self):
101-
self.N = 100000
102-
self.series = pd.Series(
103-
pd.timedelta_range('1 days', periods=self.N, freq='h'))
111+
def setup_cache(self):
112+
N = 100000
113+
series = Series(timedelta_range('1 days', periods=N, freq='h'))
114+
return series
104115

105-
def time_dt_accessor(self):
106-
self.series.dt
116+
def time_dt_accessor(self, series):
117+
series.dt
107118

108-
def time_timedelta_dt_accessor_days(self):
109-
self.series.dt.days
119+
def time_timedelta_days(self, series):
120+
series.dt.days
110121

111-
def time_timedelta_dt_accessor_seconds(self):
112-
self.series.dt.seconds
122+
def time_timedelta_seconds(self, series):
123+
series.dt.seconds
113124

114-
def time_timedelta_dt_accessor_microseconds(self):
115-
self.series.dt.microseconds
125+
def time_timedelta_microseconds(self, series):
126+
series.dt.microseconds
116127

117-
def time_timedelta_dt_accessor_nanoseconds(self):
118-
self.series.dt.nanoseconds
128+
def time_timedelta_nanoseconds(self, series):
129+
series.dt.nanoseconds

0 commit comments

Comments
 (0)