1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
"""
Period benchmarks that rely only on tslibs. See benchmarks.period for
Period benchmarks that rely on other parts of pandas.
"""
import numpy as np
from pandas._libs.tslibs.period import (
Period,
periodarr_to_dt64arr,
)
from pandas.tseries.frequencies import to_offset
from .tslib import (
_sizes,
_tzs,
tzlocal_obj,
)
try:
from pandas._libs.tslibs.vectorized import dt64arr_to_periodarr
except ImportError:
from pandas._libs.tslibs.period import dt64arr_to_periodarr
class PeriodProperties:
params = (
["M", "min"],
[
"year",
"month",
"day",
"hour",
"minute",
"second",
"is_leap_year",
"quarter",
"qyear",
"week",
"daysinmonth",
"dayofweek",
"dayofyear",
"start_time",
"end_time",
],
)
param_names = ["freq", "attr"]
def setup(self, freq, attr):
self.per = Period("2012-06-01", freq=freq)
def time_property(self, freq, attr):
getattr(self.per, attr)
class PeriodUnaryMethods:
params = ["M", "min"]
param_names = ["freq"]
def setup(self, freq):
self.per = Period("2012-06-01", freq=freq)
if freq == "M":
self.default_fmt = "%Y-%m"
elif freq == "min":
self.default_fmt = "%Y-%m-%d %H:%M"
def time_to_timestamp(self, freq):
self.per.to_timestamp()
def time_now(self, freq):
self.per.now(freq)
def time_asfreq(self, freq):
self.per.asfreq("Y")
def time_str(self, freq):
str(self.per)
def time_repr(self, freq):
repr(self.per)
def time_strftime_default(self, freq):
self.per.strftime(None)
def time_strftime_default_explicit(self, freq):
self.per.strftime(self.default_fmt)
def time_strftime_custom(self, freq):
self.per.strftime("%b. %d, %Y was a %A")
class PeriodConstructor:
params = [["D"], [True, False]]
param_names = ["freq", "is_offset"]
def setup(self, freq, is_offset):
if is_offset:
self.freq = to_offset(freq)
else:
self.freq = freq
def time_period_constructor(self, freq, is_offset):
Period("2012-06-01", freq=freq)
_freq_ints = [
1000,
1011, # Annual - November End
2000,
2011, # Quarterly - November End
3000,
4000,
4006, # Weekly - Saturday End
5000,
6000,
7000,
8000,
9000,
10000,
11000,
12000,
]
class TimePeriodArrToDT64Arr:
params = [
_sizes,
_freq_ints,
]
param_names = ["size", "freq"]
def setup(self, size, freq):
arr = np.arange(10, dtype="i8").repeat(size // 10)
self.i8values = arr
def time_periodarray_to_dt64arr(self, size, freq):
periodarr_to_dt64arr(self.i8values, freq)
class TimeDT64ArrToPeriodArr:
params = [
_sizes,
_freq_ints,
_tzs,
]
param_names = ["size", "freq", "tz"]
def setup(self, size, freq, tz):
if size == 10**6 and tz is tzlocal_obj:
# tzlocal is cumbersomely slow, so skip to keep runtime in check
raise NotImplementedError
# we pick 2**55 because smaller values end up returning
# -1 from npy_datetimestruct_to_datetime with NPY_FR_Y frequency
# this artificially slows down functions since -1 is also the
# error sentinel
arr = np.arange(2**55, 2**55 + 10, dtype="i8").repeat(size // 10)
self.i8values = arr
def time_dt64arr_to_periodarr(self, size, freq, tz):
dt64arr_to_periodarr(self.i8values, freq, tz)
|