|
| 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) |
0 commit comments