Skip to content

Commit beb1e69

Browse files
mroeschkejreback
authored andcommitted
CLN: ASV offset (pandas-dev#18926)
1 parent 2030a07 commit beb1e69

File tree

2 files changed

+66
-202
lines changed

2 files changed

+66
-202
lines changed

asv_bench/benchmarks/offset.py

+65-201
Original file line numberDiff line numberDiff line change
@@ -2,257 +2,121 @@
22
from datetime import datetime
33

44
import numpy as np
5-
65
import pandas as pd
7-
from pandas import date_range
8-
96
try:
10-
import pandas.tseries.holiday
7+
import pandas.tseries.holiday # noqa
118
except ImportError:
129
pass
1310

1411
hcal = pd.tseries.holiday.USFederalHolidayCalendar()
12+
# These offests currently raise a NotImplimentedError with .apply_index()
13+
non_apply = [pd.offsets.Day(),
14+
pd.offsets.BYearEnd(),
15+
pd.offsets.BYearBegin(),
16+
pd.offsets.BQuarterEnd(),
17+
pd.offsets.BQuarterBegin(),
18+
pd.offsets.BMonthEnd(),
19+
pd.offsets.BMonthBegin(),
20+
pd.offsets.CustomBusinessDay(),
21+
pd.offsets.CustomBusinessDay(calendar=hcal),
22+
pd.offsets.CustomBusinessMonthBegin(calendar=hcal),
23+
pd.offsets.CustomBusinessMonthEnd(calendar=hcal),
24+
pd.offsets.CustomBusinessMonthEnd(calendar=hcal)]
25+
other_offsets = [pd.offsets.YearEnd(), pd.offsets.YearBegin(),
26+
pd.offsets.QuarterEnd(), pd.offsets.QuarterBegin(),
27+
pd.offsets.MonthEnd(), pd.offsets.MonthBegin(),
28+
pd.offsets.DateOffset(months=2, days=2),
29+
pd.offsets.BusinessDay(), pd.offsets.SemiMonthEnd(),
30+
pd.offsets.SemiMonthBegin()]
31+
offsets = non_apply + other_offsets
1532

1633

1734
class ApplyIndex(object):
18-
goal_time = 0.2
1935

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
36+
goal_time = 0.2
2937

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)
38+
params = other_offsets
39+
param_names = ['offset']
3340

34-
def time_apply_index(self, param):
35-
self.rng + self.offset
41+
def setup(self, offset):
42+
N = 10000
43+
self.rng = pd.date_range(start='1/1/2000', periods=N, freq='T')
3644

37-
def time_apply_series(self, param):
38-
self.ser + self.offset
45+
def time_apply_index(self, offset):
46+
offset.apply_index(self.rng)
3947

4048

4149
class OnOffset(object):
50+
4251
goal_time = 0.2
4352

44-
params = [pd.offsets.QuarterBegin(), pd.offsets.QuarterEnd(),
45-
pd.offsets.BQuarterBegin(), pd.offsets.BQuarterEnd()]
53+
params = offsets
4654
param_names = ['offset']
4755

4856
def setup(self, offset):
49-
self.offset = offset
5057
self.dates = [datetime(2016, m, d)
5158
for m in [10, 11, 12]
5259
for d in [1, 2, 3, 28, 29, 30, 31]
5360
if not (m == 11 and d == 31)]
5461

5562
def time_on_offset(self, offset):
5663
for date in self.dates:
57-
self.offset.onOffset(date)
58-
59-
60-
class DatetimeIndexArithmetic(object):
61-
goal_time = 0.2
62-
63-
def setup(self):
64-
self.N = 100000
65-
self.rng = date_range(start='1/1/2000', periods=self.N, freq='T')
66-
self.day_offset = pd.offsets.Day()
67-
self.relativedelta_offset = pd.offsets.DateOffset(months=2, days=2)
68-
self.busday_offset = pd.offsets.BusinessDay()
69-
70-
def time_add_offset_delta(self):
71-
self.rng + self.day_offset
72-
73-
def time_add_offset_fast(self):
74-
self.rng + self.relativedelta_offset
75-
76-
def time_add_offset_slow(self):
77-
self.rng + self.busday_offset
78-
79-
80-
class SeriesArithmetic(object):
81-
goal_time = 0.2
64+
offset.onOffset(date)
8265

83-
def setup(self):
84-
self.N = 100000
85-
rng = date_range(start='20140101', freq='T', periods=self.N)
86-
self.ser = pd.Series(rng)
87-
self.day_offset = pd.offsets.Day()
88-
self.relativedelta_offset = pd.offsets.DateOffset(months=2, days=2)
89-
self.busday_offset = pd.offsets.BusinessDay()
9066

91-
def time_add_offset_delta(self):
92-
self.ser + self.day_offset
67+
class OffsetSeriesArithmetic(object):
9368

94-
def time_add_offset_fast(self):
95-
self.ser + self.relativedelta_offset
96-
97-
def time_add_offset_slow(self):
98-
self.ser + self.busday_offset
99-
100-
101-
class YearBegin(object):
10269
goal_time = 0.2
70+
params = offsets
71+
param_names = ['offset']
10372

104-
def setup(self):
105-
self.date = datetime(2011, 1, 1)
106-
self.year = pd.offsets.YearBegin()
73+
def setup(self, offset):
74+
N = 1000
75+
rng = pd.date_range(start='1/1/2000', periods=N, freq='T')
76+
self.data = pd.Series(rng)
10777

108-
def time_timeseries_year_apply(self):
109-
self.year.apply(self.date)
78+
def time_add_offset(self, offset):
79+
self.data + offset
11080

111-
def time_timeseries_year_incr(self):
112-
self.date + self.year
11381

82+
class OffsetDatetimeIndexArithmetic(object):
11483

115-
class Day(object):
11684
goal_time = 0.2
85+
params = offsets
86+
param_names = ['offset']
11787

118-
def setup(self):
119-
self.date = datetime(2011, 1, 1)
120-
self.day = pd.offsets.Day()
88+
def setup(self, offset):
89+
N = 1000
90+
self.data = pd.date_range(start='1/1/2000', periods=N, freq='T')
12191

122-
def time_timeseries_day_apply(self):
123-
self.day.apply(self.date)
92+
def time_add_offset(self, offset):
93+
self.data + offset
12494

125-
def time_timeseries_day_incr(self):
126-
self.date + self.day
12795

96+
class OffestDatetimeArithmetic(object):
12897

129-
class CBDay(object):
13098
goal_time = 0.2
99+
params = offsets
100+
param_names = ['offset']
131101

132-
def setup(self):
102+
def setup(self, offset):
133103
self.date = datetime(2011, 1, 1)
134104
self.dt64 = np.datetime64('2011-01-01 09:00Z')
135-
self.cday = pd.offsets.CustomBusinessDay()
136-
137-
def time_custom_bday_decr(self):
138-
self.date - self.cday
139-
140-
def time_custom_bday_incr(self):
141-
self.date + self.cday
142-
143-
def time_custom_bday_apply(self):
144-
self.cday.apply(self.date)
145-
146-
def time_custom_bday_apply_dt64(self):
147-
self.cday.apply(self.dt64)
148-
149-
150-
class CBDayHolidays(object):
151-
goal_time = 0.2
152-
153-
def setup(self):
154-
self.date = datetime(2011, 1, 1)
155-
self.cdayh = pd.offsets.CustomBusinessDay(calendar=hcal)
156-
157-
def time_custom_bday_cal_incr(self):
158-
self.date + 1 * self.cdayh
159-
160-
def time_custom_bday_cal_decr(self):
161-
self.date - 1 * self.cdayh
162-
163-
def time_custom_bday_cal_incr_n(self):
164-
self.date + 10 * self.cdayh
165-
166-
def time_custom_bday_cal_incr_neg_n(self):
167-
self.date - 10 * self.cdayh
168-
169-
170-
class CBMonthBegin(object):
171-
goal_time = 0.2
172-
173-
def setup(self):
174-
self.date = datetime(2011, 1, 1)
175-
self.cmb = pd.offsets.CustomBusinessMonthBegin(calendar=hcal)
176-
177-
def time_custom_bmonthbegin_decr_n(self):
178-
self.date - (10 * self.cmb)
179-
180-
def time_custom_bmonthbegin_incr_n(self):
181-
self.date + (10 * self.cmb)
182-
183-
184-
class CBMonthEnd(object):
185-
goal_time = 0.2
186-
187-
def setup(self):
188-
self.date = datetime(2011, 1, 1)
189-
self.cme = pd.offsets.CustomBusinessMonthEnd(calendar=hcal)
190-
191-
def time_custom_bmonthend_incr(self):
192-
self.date + self.cme
193-
194-
def time_custom_bmonthend_incr_n(self):
195-
self.date + (10 * self.cme)
196-
197-
def time_custom_bmonthend_decr_n(self):
198-
self.date - (10 * self.cme)
199-
200-
201-
class SemiMonthOffset(object):
202-
goal_time = 0.2
203-
204-
def setup(self):
205-
self.N = 100000
206-
self.rng = date_range(start='1/1/2000', periods=self.N, freq='T')
207-
# date is not on an offset which will be slowest case
208-
self.date = datetime(2011, 1, 2)
209-
self.semi_month_end = pd.offsets.SemiMonthEnd()
210-
self.semi_month_begin = pd.offsets.SemiMonthBegin()
211-
212-
def time_end_apply(self):
213-
self.semi_month_end.apply(self.date)
214-
215-
def time_end_incr(self):
216-
self.date + self.semi_month_end
217-
218-
def time_end_incr_n(self):
219-
self.date + 10 * self.semi_month_end
220-
221-
def time_end_decr(self):
222-
self.date - self.semi_month_end
223-
224-
def time_end_decr_n(self):
225-
self.date - 10 * self.semi_month_end
226-
227-
def time_end_apply_index(self):
228-
self.semi_month_end.apply_index(self.rng)
229-
230-
def time_end_incr_rng(self):
231-
self.rng + self.semi_month_end
232-
233-
def time_end_decr_rng(self):
234-
self.rng - self.semi_month_end
235-
236-
def time_begin_apply(self):
237-
self.semi_month_begin.apply(self.date)
238-
239-
def time_begin_incr(self):
240-
self.date + self.semi_month_begin
241105

242-
def time_begin_incr_n(self):
243-
self.date + 10 * self.semi_month_begin
106+
def time_apply(self, offset):
107+
offset.apply(self.date)
244108

245-
def time_begin_decr(self):
246-
self.date - self.semi_month_begin
109+
def time_apply_np_dt64(self, offset):
110+
offset.apply(self.dt64)
247111

248-
def time_begin_decr_n(self):
249-
self.date - 10 * self.semi_month_begin
112+
def time_add(self, offset):
113+
self.date + offset
250114

251-
def time_begin_apply_index(self):
252-
self.semi_month_begin.apply_index(self.rng)
115+
def time_add_10(self, offset):
116+
self.date + (10 * offset)
253117

254-
def time_begin_incr_rng(self):
255-
self.rng + self.semi_month_begin
118+
def time_subtract(self, offset):
119+
self.date - offset
256120

257-
def time_begin_decr_rng(self):
258-
self.rng - self.semi_month_begin
121+
def time_subtract_10(self, offset):
122+
self.date - (10 * offset)

ci/lint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if [ "$LINT" ]; then
2424
echo "Linting setup.py DONE"
2525

2626
echo "Linting asv_bench/benchmarks/"
27-
flake8 asv_bench/benchmarks/ --exclude=asv_bench/benchmarks/[ijoprs]*.py --ignore=F811
27+
flake8 asv_bench/benchmarks/ --exclude=asv_bench/benchmarks/[ips]*.py --ignore=F811
2828
if [ $? -ne "0" ]; then
2929
RET=1
3030
fi

0 commit comments

Comments
 (0)