forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseries_methods.py
204 lines (140 loc) · 5.62 KB
/
series_methods.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from datetime import datetime
import numpy as np
import pandas.util.testing as tm
from pandas import Series, date_range, NaT
class SeriesConstructor:
params = [None, 'dict']
param_names = ['data']
def setup(self, data):
self.idx = date_range(start=datetime(2015, 10, 26),
end=datetime(2016, 1, 1),
freq='50s')
dict_data = dict(zip(self.idx, range(len(self.idx))))
self.data = None if data is None else dict_data
def time_constructor(self, data):
Series(data=self.data, index=self.idx)
class IsIn:
params = ['int64', 'uint64', 'object']
param_names = ['dtype']
def setup(self, dtype):
self.s = Series(np.random.randint(1, 10, 100000)).astype(dtype)
self.values = [1, 2]
def time_isin(self, dtypes):
self.s.isin(self.values)
class IsInFloat64:
def setup(self):
self.small = Series([1, 2], dtype=np.float64)
self.many_different_values = np.arange(10**6, dtype=np.float64)
self.few_different_values = np.zeros(10**7, dtype=np.float64)
self.only_nans_values = np.full(10**7, np.nan, dtype=np.float64)
def time_isin_many_different(self):
# runtime is dominated by creation of the lookup-table
self.small.isin(self.many_different_values)
def time_isin_few_different(self):
# runtime is dominated by creation of the lookup-table
self.small.isin(self.few_different_values)
def time_isin_nan_values(self):
# runtime is dominated by creation of the lookup-table
self.small.isin(self.few_different_values)
class IsInForObjects:
def setup(self):
self.s_nans = Series(np.full(10**4, np.nan)).astype(np.object)
self.vals_nans = np.full(10**4, np.nan).astype(np.object)
self.s_short = Series(np.arange(2)).astype(np.object)
self.s_long = Series(np.arange(10**5)).astype(np.object)
self.vals_short = np.arange(2).astype(np.object)
self.vals_long = np.arange(10**5).astype(np.object)
# because of nans floats are special:
self.s_long_floats = Series(np.arange(10**5,
dtype=np.float)).astype(np.object)
self.vals_long_floats = np.arange(10**5,
dtype=np.float).astype(np.object)
def time_isin_nans(self):
# if nan-objects are different objects,
# this has the potential to trigger O(n^2) running time
self.s_nans.isin(self.vals_nans)
def time_isin_short_series_long_values(self):
# running time dominated by the preprocessing
self.s_short.isin(self.vals_long)
def time_isin_long_series_short_values(self):
# running time dominated by look-up
self.s_long.isin(self.vals_short)
def time_isin_long_series_long_values(self):
# no dominating part
self.s_long.isin(self.vals_long)
def time_isin_long_series_long_values_floats(self):
# no dominating part
self.s_long_floats.isin(self.vals_long_floats)
class NSort:
params = ['first', 'last', 'all']
param_names = ['keep']
def setup(self, keep):
self.s = Series(np.random.randint(1, 10, 100000))
def time_nlargest(self, keep):
self.s.nlargest(3, keep=keep)
def time_nsmallest(self, keep):
self.s.nsmallest(3, keep=keep)
class Dropna:
params = ['int', 'datetime']
param_names = ['dtype']
def setup(self, dtype):
N = 10**6
data = {'int': np.random.randint(1, 10, N),
'datetime': date_range('2000-01-01', freq='S', periods=N)}
self.s = Series(data[dtype])
if dtype == 'datetime':
self.s[np.random.randint(1, N, 100)] = NaT
def time_dropna(self, dtype):
self.s.dropna()
class SearchSorted:
goal_time = 0.2
params = ['int8', 'int16', 'int32', 'int64',
'uint8', 'uint16', 'uint32', 'uint64',
'float16', 'float32', 'float64',
'str']
param_names = ['dtype']
def setup(self, dtype):
N = 10**5
data = np.array([1] * N + [2] * N + [3] * N).astype(dtype)
self.s = Series(data)
def time_searchsorted(self, dtype):
key = '2' if dtype == 'str' else 2
self.s.searchsorted(key)
class Map:
params = ['dict', 'Series']
param_names = 'mapper'
def setup(self, mapper):
map_size = 1000
map_data = Series(map_size - np.arange(map_size))
self.map_data = map_data if mapper == 'Series' else map_data.to_dict()
self.s = Series(np.random.randint(0, map_size, 10000))
def time_map(self, mapper):
self.s.map(self.map_data)
class Clip:
params = [50, 1000, 10**5]
param_names = ['n']
def setup(self, n):
self.s = Series(np.random.randn(n))
def time_clip(self, n):
self.s.clip(0, 1)
class ValueCounts:
params = ['int', 'uint', 'float', 'object']
param_names = ['dtype']
def setup(self, dtype):
self.s = Series(np.random.randint(0, 1000, size=100000)).astype(dtype)
def time_value_counts(self, dtype):
self.s.value_counts()
class Dir:
def setup(self):
self.s = Series(index=tm.makeStringIndex(10000))
def time_dir_strings(self):
dir(self.s)
class SeriesGetattr:
# https://github.com/pandas-dev/pandas/issues/19764
def setup(self):
self.s = Series(1,
index=date_range("2012-01-01", freq='s',
periods=int(1e6)))
def time_series_datetimeindex_repr(self):
getattr(self.s, 'a', None)
from .pandas_vb_common import setup # noqa: F401