Skip to content

Commit 78b24b2

Browse files
jbrockmendeljreback
authored andcommitted
parametrize offsets tests (#18494)
1 parent b08c22b commit 78b24b2

File tree

6 files changed

+2905
-2746
lines changed

6 files changed

+2905
-2746
lines changed

asv_bench/benchmarks/offset.py

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
# -*- coding: utf-8 -*-
2+
from datetime import datetime
3+
4+
import numpy as np
5+
6+
import pandas as pd
7+
from pandas import date_range
8+
9+
try:
10+
import pandas.tseries.holiday
11+
except ImportError:
12+
pass
13+
14+
hcal = pd.tseries.holiday.USFederalHolidayCalendar()
15+
16+
17+
class ApplyIndex(object):
18+
goal_time = 0.2
19+
20+
params = [pd.offsets.YearEnd(), pd.offsets.YearBegin(),
21+
pd.offsets.BYearEnd(), pd.offsets.BYearBegin(),
22+
pd.offsets.QuarterEnd(), pd.offsets.QuarterBegin(),
23+
pd.offsets.BQuarterEnd(), pd.offsets.BQuarterBegin(),
24+
pd.offsets.MonthEnd(), pd.offsets.MonthBegin(),
25+
pd.offsets.BMonthEnd(), pd.offsets.BMonthBegin()]
26+
27+
def setup(self, param):
28+
self.offset = param
29+
30+
self.N = 100000
31+
self.rng = date_range(start='1/1/2000', periods=self.N, freq='T')
32+
self.ser = pd.Series(self.rng)
33+
34+
def time_apply_index(self, param):
35+
self.rng + self.offset
36+
37+
def time_apply_series(self, param):
38+
self.ser + self.offset
39+
40+
41+
class DatetimeIndexArithmetic(object):
42+
goal_time = 0.2
43+
44+
def setup(self):
45+
self.N = 100000
46+
self.rng = date_range(start='1/1/2000', periods=self.N, freq='T')
47+
self.day_offset = pd.offsets.Day()
48+
self.relativedelta_offset = pd.offsets.DateOffset(months=2, days=2)
49+
self.busday_offset = pd.offsets.BusinessDay()
50+
51+
def time_add_offset_delta(self):
52+
self.rng + self.day_offset
53+
54+
def time_add_offset_fast(self):
55+
self.rng + self.relativedelta_offset
56+
57+
def time_add_offset_slow(self):
58+
self.rng + self.busday_offset
59+
60+
61+
class SeriesArithmetic(object):
62+
goal_time = 0.2
63+
64+
def setup(self):
65+
self.N = 100000
66+
rng = date_range(start='20140101', freq='T', periods=self.N)
67+
self.ser = pd.Series(rng)
68+
self.day_offset = pd.offsets.Day()
69+
self.relativedelta_offset = pd.offsets.DateOffset(months=2, days=2)
70+
self.busday_offset = pd.offsets.BusinessDay()
71+
72+
def time_add_offset_delta(self):
73+
self.ser + self.day_offset
74+
75+
def time_add_offset_fast(self):
76+
self.ser + self.relativedelta_offset
77+
78+
def time_add_offset_slow(self):
79+
self.ser + self.busday_offset
80+
81+
82+
class YearBegin(object):
83+
goal_time = 0.2
84+
85+
def setup(self):
86+
self.date = datetime(2011, 1, 1)
87+
self.year = pd.offsets.YearBegin()
88+
89+
def time_timeseries_year_apply(self):
90+
self.year.apply(self.date)
91+
92+
def time_timeseries_year_incr(self):
93+
self.date + self.year
94+
95+
96+
class Day(object):
97+
goal_time = 0.2
98+
99+
def setup(self):
100+
self.date = datetime(2011, 1, 1)
101+
self.day = pd.offsets.Day()
102+
103+
def time_timeseries_day_apply(self):
104+
self.day.apply(self.date)
105+
106+
def time_timeseries_day_incr(self):
107+
self.date + self.day
108+
109+
110+
class CBDay(object):
111+
goal_time = 0.2
112+
113+
def setup(self):
114+
self.date = datetime(2011, 1, 1)
115+
self.dt64 = np.datetime64('2011-01-01 09:00Z')
116+
self.cday = pd.offsets.CustomBusinessDay()
117+
118+
def time_custom_bday_decr(self):
119+
self.date - self.cday
120+
121+
def time_custom_bday_incr(self):
122+
self.date + self.cday
123+
124+
def time_custom_bday_apply(self):
125+
self.cday.apply(self.date)
126+
127+
def time_custom_bday_apply_dt64(self):
128+
self.cday.apply(self.dt64)
129+
130+
131+
class CBDayHolidays(object):
132+
goal_time = 0.2
133+
134+
def setup(self):
135+
self.date = datetime(2011, 1, 1)
136+
self.cdayh = pd.offsets.CustomBusinessDay(calendar=hcal)
137+
138+
def time_custom_bday_cal_incr(self):
139+
self.date + 1 * self.cdayh
140+
141+
def time_custom_bday_cal_decr(self):
142+
self.date - 1 * self.cdayh
143+
144+
def time_custom_bday_cal_incr_n(self):
145+
self.date + 10 * self.cdayh
146+
147+
def time_custom_bday_cal_incr_neg_n(self):
148+
self.date - 10 * self.cdayh
149+
150+
151+
class CBMonthBegin(object):
152+
goal_time = 0.2
153+
154+
def setup(self):
155+
self.date = datetime(2011, 1, 1)
156+
self.cmb = pd.offsets.CustomBusinessMonthBegin(calendar=hcal)
157+
158+
def time_custom_bmonthbegin_decr_n(self):
159+
self.date - (10 * self.cmb)
160+
161+
def time_custom_bmonthbegin_incr_n(self):
162+
self.date + (10 * self.cmb)
163+
164+
165+
class CBMonthEnd(object):
166+
goal_time = 0.2
167+
168+
def setup(self):
169+
self.date = datetime(2011, 1, 1)
170+
self.cme = pd.offsets.CustomBusinessMonthEnd(calendar=hcal)
171+
172+
def time_custom_bmonthend_incr(self):
173+
self.date + self.cme
174+
175+
def time_custom_bmonthend_incr_n(self):
176+
self.date + (10 * self.cme)
177+
178+
def time_custom_bmonthend_decr_n(self):
179+
self.date - (10 * self.cme)
180+
181+
182+
class SemiMonthOffset(object):
183+
goal_time = 0.2
184+
185+
def setup(self):
186+
self.N = 100000
187+
self.rng = date_range(start='1/1/2000', periods=self.N, freq='T')
188+
# date is not on an offset which will be slowest case
189+
self.date = datetime(2011, 1, 2)
190+
self.semi_month_end = pd.offsets.SemiMonthEnd()
191+
self.semi_month_begin = pd.offsets.SemiMonthBegin()
192+
193+
def time_end_apply(self):
194+
self.semi_month_end.apply(self.date)
195+
196+
def time_end_incr(self):
197+
self.date + self.semi_month_end
198+
199+
def time_end_incr_n(self):
200+
self.date + 10 * self.semi_month_end
201+
202+
def time_end_decr(self):
203+
self.date - self.semi_month_end
204+
205+
def time_end_decr_n(self):
206+
self.date - 10 * self.semi_month_end
207+
208+
def time_end_apply_index(self):
209+
self.semi_month_end.apply_index(self.rng)
210+
211+
def time_end_incr_rng(self):
212+
self.rng + self.semi_month_end
213+
214+
def time_end_decr_rng(self):
215+
self.rng - self.semi_month_end
216+
217+
def time_begin_apply(self):
218+
self.semi_month_begin.apply(self.date)
219+
220+
def time_begin_incr(self):
221+
self.date + self.semi_month_begin
222+
223+
def time_begin_incr_n(self):
224+
self.date + 10 * self.semi_month_begin
225+
226+
def time_begin_decr(self):
227+
self.date - self.semi_month_begin
228+
229+
def time_begin_decr_n(self):
230+
self.date - 10 * self.semi_month_begin
231+
232+
def time_begin_apply_index(self):
233+
self.semi_month_begin.apply_index(self.rng)
234+
235+
def time_begin_incr_rng(self):
236+
self.rng + self.semi_month_begin
237+
238+
def time_begin_decr_rng(self):
239+
self.rng - self.semi_month_begin

0 commit comments

Comments
 (0)