Skip to content

Commit 3c0cf22

Browse files
jbrockmendelWillAyd
authored andcommitted
REF/BENCH: tslibs-specific parts of asvs (#29292)
1 parent 3598a5e commit 3c0cf22

File tree

8 files changed

+237
-173
lines changed

8 files changed

+237
-173
lines changed

asv_bench/benchmarks/offset.py

-49
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from datetime import datetime
21
import warnings
32

4-
import numpy as np
5-
63
import pandas as pd
74

85
try:
@@ -54,24 +51,6 @@ def time_apply_index(self, offset):
5451
offset.apply_index(self.rng)
5552

5653

57-
class OnOffset:
58-
59-
params = offsets
60-
param_names = ["offset"]
61-
62-
def setup(self, offset):
63-
self.dates = [
64-
datetime(2016, m, d)
65-
for m in [10, 11, 12]
66-
for d in [1, 2, 3, 28, 29, 30, 31]
67-
if not (m == 11 and d == 31)
68-
]
69-
70-
def time_on_offset(self, offset):
71-
for date in self.dates:
72-
offset.onOffset(date)
73-
74-
7554
class OffsetSeriesArithmetic:
7655

7756
params = offsets
@@ -99,31 +78,3 @@ def setup(self, offset):
9978
def time_add_offset(self, offset):
10079
with warnings.catch_warnings(record=True):
10180
self.data + offset
102-
103-
104-
class OffestDatetimeArithmetic:
105-
106-
params = offsets
107-
param_names = ["offset"]
108-
109-
def setup(self, offset):
110-
self.date = datetime(2011, 1, 1)
111-
self.dt64 = np.datetime64("2011-01-01 09:00Z")
112-
113-
def time_apply(self, offset):
114-
offset.apply(self.date)
115-
116-
def time_apply_np_dt64(self, offset):
117-
offset.apply(self.dt64)
118-
119-
def time_add(self, offset):
120-
self.date + offset
121-
122-
def time_add_10(self, offset):
123-
self.date + (10 * offset)
124-
125-
def time_subtract(self, offset):
126-
self.date - offset
127-
128-
def time_subtract_10(self, offset):
129-
self.date - (10 * offset)

asv_bench/benchmarks/period.py

+4-63
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,12 @@
1+
"""
2+
Period benchmarks with non-tslibs dependencies. See
3+
benchmarks.tslibs.period for benchmarks that rely only on tslibs.
4+
"""
15
from pandas import DataFrame, Period, PeriodIndex, Series, date_range, period_range
26

37
from pandas.tseries.frequencies import to_offset
48

59

6-
class PeriodProperties:
7-
8-
params = (
9-
["M", "min"],
10-
[
11-
"year",
12-
"month",
13-
"day",
14-
"hour",
15-
"minute",
16-
"second",
17-
"is_leap_year",
18-
"quarter",
19-
"qyear",
20-
"week",
21-
"daysinmonth",
22-
"dayofweek",
23-
"dayofyear",
24-
"start_time",
25-
"end_time",
26-
],
27-
)
28-
param_names = ["freq", "attr"]
29-
30-
def setup(self, freq, attr):
31-
self.per = Period("2012-06-01", freq=freq)
32-
33-
def time_property(self, freq, attr):
34-
getattr(self.per, attr)
35-
36-
37-
class PeriodUnaryMethods:
38-
39-
params = ["M", "min"]
40-
param_names = ["freq"]
41-
42-
def setup(self, freq):
43-
self.per = Period("2012-06-01", freq=freq)
44-
45-
def time_to_timestamp(self, freq):
46-
self.per.to_timestamp()
47-
48-
def time_now(self, freq):
49-
self.per.now(freq)
50-
51-
def time_asfreq(self, freq):
52-
self.per.asfreq("A")
53-
54-
55-
class PeriodConstructor:
56-
params = [["D"], [True, False]]
57-
param_names = ["freq", "is_offset"]
58-
59-
def setup(self, freq, is_offset):
60-
if is_offset:
61-
self.freq = to_offset(freq)
62-
else:
63-
self.freq = freq
64-
65-
def time_period_constructor(self, freq, is_offset):
66-
Period("2012-06-01", freq=freq)
67-
68-
6910
class PeriodIndexConstructor:
7011

7112
params = [["D"], [True, False]]

asv_bench/benchmarks/timedelta.py

+5-61
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
1-
import datetime
1+
"""
2+
Timedelta benchmarks with non-tslibs dependencies. See
3+
benchmarks.tslibs.timedelta for benchmarks that rely only on tslibs.
4+
"""
25

36
import numpy as np
47

5-
from pandas import (
6-
DataFrame,
7-
Series,
8-
Timedelta,
9-
Timestamp,
10-
timedelta_range,
11-
to_timedelta,
12-
)
13-
14-
15-
class TimedeltaConstructor:
16-
def time_from_int(self):
17-
Timedelta(123456789)
18-
19-
def time_from_unit(self):
20-
Timedelta(1, unit="d")
21-
22-
def time_from_components(self):
23-
Timedelta(
24-
days=1,
25-
hours=2,
26-
minutes=3,
27-
seconds=4,
28-
milliseconds=5,
29-
microseconds=6,
30-
nanoseconds=7,
31-
)
32-
33-
def time_from_datetime_timedelta(self):
34-
Timedelta(datetime.timedelta(days=1, seconds=1))
35-
36-
def time_from_np_timedelta(self):
37-
Timedelta(np.timedelta64(1, "ms"))
38-
39-
def time_from_string(self):
40-
Timedelta("1 days")
41-
42-
def time_from_iso_format(self):
43-
Timedelta("P4DT12H30M5S")
44-
45-
def time_from_missing(self):
46-
Timedelta("nat")
8+
from pandas import DataFrame, Series, Timestamp, timedelta_range, to_timedelta
479

4810

4911
class ToTimedelta:
@@ -88,24 +50,6 @@ def time_add_td_ts(self):
8850
self.td + self.ts
8951

9052

91-
class TimedeltaProperties:
92-
def setup_cache(self):
93-
td = Timedelta(days=365, minutes=35, seconds=25, milliseconds=35)
94-
return td
95-
96-
def time_timedelta_days(self, td):
97-
td.days
98-
99-
def time_timedelta_seconds(self, td):
100-
td.seconds
101-
102-
def time_timedelta_microseconds(self, td):
103-
td.microseconds
104-
105-
def time_timedelta_nanoseconds(self, td):
106-
td.nanoseconds
107-
108-
10953
class DatetimeAccessor:
11054
def setup_cache(self):
11155
N = 100000
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Benchmarks in this directory should depend only on tslibs, tseries.offsets,
3+
and to_offset.
4+
5+
i.e. any code changes that do not touch those files should not need to
6+
run these benchmarks.
7+
"""
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
offsets benchmarks that rely only on tslibs. See benchmarks.offset for
3+
offsets benchmarks that rely on other parts of pandas.
4+
"""
5+
from datetime import datetime
6+
7+
import numpy as np
8+
9+
from pandas import offsets
10+
11+
try:
12+
import pandas.tseries.holiday # noqa
13+
except ImportError:
14+
pass
15+
16+
hcal = pandas.tseries.holiday.USFederalHolidayCalendar()
17+
# These offsets currently raise a NotImplimentedError with .apply_index()
18+
non_apply = [
19+
offsets.Day(),
20+
offsets.BYearEnd(),
21+
offsets.BYearBegin(),
22+
offsets.BQuarterEnd(),
23+
offsets.BQuarterBegin(),
24+
offsets.BMonthEnd(),
25+
offsets.BMonthBegin(),
26+
offsets.CustomBusinessDay(),
27+
offsets.CustomBusinessDay(calendar=hcal),
28+
offsets.CustomBusinessMonthBegin(calendar=hcal),
29+
offsets.CustomBusinessMonthEnd(calendar=hcal),
30+
offsets.CustomBusinessMonthEnd(calendar=hcal),
31+
]
32+
other_offsets = [
33+
offsets.YearEnd(),
34+
offsets.YearBegin(),
35+
offsets.QuarterEnd(),
36+
offsets.QuarterBegin(),
37+
offsets.MonthEnd(),
38+
offsets.MonthBegin(),
39+
offsets.DateOffset(months=2, days=2),
40+
offsets.BusinessDay(),
41+
offsets.SemiMonthEnd(),
42+
offsets.SemiMonthBegin(),
43+
]
44+
offset_objs = non_apply + other_offsets
45+
46+
47+
class OnOffset:
48+
49+
params = offset_objs
50+
param_names = ["offset"]
51+
52+
def setup(self, offset):
53+
self.dates = [
54+
datetime(2016, m, d)
55+
for m in [10, 11, 12]
56+
for d in [1, 2, 3, 28, 29, 30, 31]
57+
if not (m == 11 and d == 31)
58+
]
59+
60+
def time_on_offset(self, offset):
61+
for date in self.dates:
62+
offset.onOffset(date)
63+
64+
65+
class OffestDatetimeArithmetic:
66+
67+
params = offset_objs
68+
param_names = ["offset"]
69+
70+
def setup(self, offset):
71+
self.date = datetime(2011, 1, 1)
72+
self.dt64 = np.datetime64("2011-01-01 09:00Z")
73+
74+
def time_apply(self, offset):
75+
offset.apply(self.date)
76+
77+
def time_apply_np_dt64(self, offset):
78+
offset.apply(self.dt64)
79+
80+
def time_add(self, offset):
81+
self.date + offset
82+
83+
def time_add_10(self, offset):
84+
self.date + (10 * offset)
85+
86+
def time_subtract(self, offset):
87+
self.date - offset
88+
89+
def time_subtract_10(self, offset):
90+
self.date - (10 * offset)

asv_bench/benchmarks/tslibs/period.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Period benchmarks that rely only on tslibs. See benchmarks.period for
3+
Period benchmarks that rely on other parts fo pandas.
4+
"""
5+
from pandas import Period
6+
7+
from pandas.tseries.frequencies import to_offset
8+
9+
10+
class PeriodProperties:
11+
12+
params = (
13+
["M", "min"],
14+
[
15+
"year",
16+
"month",
17+
"day",
18+
"hour",
19+
"minute",
20+
"second",
21+
"is_leap_year",
22+
"quarter",
23+
"qyear",
24+
"week",
25+
"daysinmonth",
26+
"dayofweek",
27+
"dayofyear",
28+
"start_time",
29+
"end_time",
30+
],
31+
)
32+
param_names = ["freq", "attr"]
33+
34+
def setup(self, freq, attr):
35+
self.per = Period("2012-06-01", freq=freq)
36+
37+
def time_property(self, freq, attr):
38+
getattr(self.per, attr)
39+
40+
41+
class PeriodUnaryMethods:
42+
43+
params = ["M", "min"]
44+
param_names = ["freq"]
45+
46+
def setup(self, freq):
47+
self.per = Period("2012-06-01", freq=freq)
48+
49+
def time_to_timestamp(self, freq):
50+
self.per.to_timestamp()
51+
52+
def time_now(self, freq):
53+
self.per.now(freq)
54+
55+
def time_asfreq(self, freq):
56+
self.per.asfreq("A")
57+
58+
59+
class PeriodConstructor:
60+
params = [["D"], [True, False]]
61+
param_names = ["freq", "is_offset"]
62+
63+
def setup(self, freq, is_offset):
64+
if is_offset:
65+
self.freq = to_offset(freq)
66+
else:
67+
self.freq = freq
68+
69+
def time_period_constructor(self, freq, is_offset):
70+
Period("2012-06-01", freq=freq)

0 commit comments

Comments
 (0)