/
vector.py
364 lines (249 loc) · 8.57 KB
/
vector.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import pandas as pd
import numpy as np
from functools import singledispatch
from siuba.siu import symbolic_dispatch
from pandas.core.groupby import SeriesGroupBy, GroupBy
from pandas.core.frame import NDFrame
from pandas import Series
from siuba.experimental.pd_groups.groupby import GroupByAgg, _regroup
from siuba.experimental.pd_groups.translate import method_agg_op
# Utils =======================================================================
def _expand_bool(x, f):
return x.expanding().apply(f, raw = True).astype(bool)
def alias_series_agg(name):
method = method_agg_op(name, is_property = False, accessor = False)
def decorator(dispatcher):
dispatcher.register(SeriesGroupBy, method)
return dispatcher
return decorator
# Single dispatch functions ===================================================
@symbolic_dispatch(cls = Series)
def cumall(x):
"""Return a same-length array. For each entry, indicates whether that entry and all previous are True-like.
Example:
>>> cumall(pd.Series([True, False, False]))
0 True
1 False
2 False
dtype: bool
"""
return _expand_bool(x, np.all)
@symbolic_dispatch(cls = Series)
def cumany(x):
"""Return a same-length array. For each entry, indicates whether that entry or any previous are True-like.
Example:
>>> cumany(pd.Series([False, True, False]))
0 False
1 True
2 True
dtype: bool
"""
return _expand_bool(x, np.any)
@symbolic_dispatch(cls = Series)
def cummean(x):
"""Return a same-length array, containing the cumulative mean."""
return x.expanding().mean()
@cummean.register(SeriesGroupBy)
def _cummean_grouped(x):
grouper = x.grouper
n_entries = x.obj.notna().groupby(grouper).cumsum()
res = x.cumsum() / n_entries
return res.groupby(grouper)
@symbolic_dispatch(cls = Series)
def desc(x):
"""Return array sorted in descending order."""
return x.sort_values(ascending = False).reset_index(drop = True)
@symbolic_dispatch(cls = Series)
def dense_rank(x):
"""Return the dense rank.
This method of ranking returns values ranging from 1 to the number of unique entries.
Ties are all given the same ranking.
Example:
>>> dense_rank(pd.Series([1,3,3,5]))
0 1.0
1 2.0
2 2.0
3 3.0
dtype: float64
"""
return x.rank(method = "dense")
@symbolic_dispatch(cls = Series)
def percent_rank(x):
"""TODO: Not Implemented"""
NotImplementedError("PRs welcome")
@symbolic_dispatch(cls = Series)
def min_rank(x):
"""Return the min rank. See pd.Series.rank for details.
"""
return x.rank(method = "min")
@symbolic_dispatch(cls = Series)
def cume_dist(x):
"""Return the cumulative distribution corresponding to each value in x.
This reflects the proportion of values that are less than or equal to each value.
"""
return x.rank(method = "max") / x.count()
# row_number ------------------------------------------------------------------
@symbolic_dispatch(cls = NDFrame)
def row_number(x):
"""Return the row number (position) for each value in x, beginning with 1.
Example:
>>> row_number(pd.Series([7,8,9]))
0 1
1 2
2 3
dtype: int64
"""
if isinstance(x, pd.DataFrame):
n = x.shape[0]
else:
n = len(x)
arr = np.arange(1, n + 1)
# could use single dispatch, but for now ensure output data type matches input
if isinstance(x, pd.Series):
return x._constructor(arr, pd.RangeIndex(n), fastpath = True)
return pd.Series(arr)
@row_number.register(GroupBy)
def _row_number_grouped(g: GroupBy) -> GroupBy:
out = np.ones(len(g.obj), dtype = int)
indices = g.grouper.indices
for g_key, inds in indices.items():
out[inds] = np.arange(1, len(inds) + 1, dtype = int)
return _regroup(out, g)
# ntile -----------------------------------------------------------------------
@symbolic_dispatch(cls = Series)
def ntile(x, n):
"""TODO: Not Implemented"""
NotImplementedError("ntile not implemented")
# between ---------------------------------------------------------------------
@symbolic_dispatch(cls = Series)
def between(x, left, right):
"""Return whether a value is between left and right (including either side).
Example:
>>> between(pd.Series([1,2,3]), 0, 2)
0 True
1 True
2 False
dtype: bool
Note:
This is a thin wrapper around pd.Series.between(left, right)
"""
# note: NA -> False, in tidyverse NA -> NA
return x.between(left, right)
# coalesce --------------------------------------------------------------------
@symbolic_dispatch(cls = Series)
def coalesce(*args):
"""TODO: Not Implemented"""
NotImplementedError("coalesce not implemented")
# lead ------------------------------------------------------------------------
@symbolic_dispatch(cls = Series)
def lead(x, n = 1, default = None):
"""Return an array with each value replaced by the next (or further forward) value in the array.
Arguments:
x: a pandas Series object
n: number of next values forward to replace each value with
default: what to replace the n final values of the array with
Example:
>>> lead(pd.Series([1,2,3]), n=1)
0 2.0
1 3.0
2 NaN
dtype: float64
>>> lead(pd.Series([1,2,3]), n=1, default = 99)
0 2
1 3
2 99
dtype: int64
"""
res = x.shift(-1*n, fill_value = default)
return res
@lead.register(SeriesGroupBy)
def _lead_grouped(x, n = 1, default = None):
res = x.shift(-1*n, fill_value = default)
return _regroup(res, x)
# lag -------------------------------------------------------------------------
@symbolic_dispatch(cls = Series)
def lag(x, n = 1, default = None):
"""Return an array with each value replaced by the previous (or further backward) value in the array.
Arguments:
x: a pandas Series object
n: number of next values backward to replace each value with
default: what to replace the n final values of the array with
Example:
>>> lag(pd.Series([1,2,3]), n=1)
0 NaN
1 1.0
2 2.0
dtype: float64
>>> lag(pd.Series([1,2,3]), n=1, default = 99)
0 99.0
1 1.0
2 2.0
dtype: float64
"""
res = x.shift(n)
if default is not None:
res.iloc[:n] = default
return res
@lag.register(SeriesGroupBy)
def _lag_grouped(x, n = 1, default = None):
res = x.shift(n, fill_value = default)
return _regroup(res, x)
# n ---------------------------------------------------------------------------
@symbolic_dispatch(cls = NDFrame)
def n(x):
"""Return the total number of elements in the array (or rows in a DataFrame).
Example:
>>> ser = pd.Series([1,2,3])
>>> n(ser)
3
>>> df = pd.DataFrame({'x': ser})
>>> n(df)
3
"""
if isinstance(x, pd.DataFrame):
return x.shape[0]
return len(x)
@n.register(GroupBy)
def _n_grouped(x: GroupBy) -> GroupByAgg:
return GroupByAgg.from_result(x.size(), x)
# n_distinct ------------------------------------------------------------------
@alias_series_agg('nunique')
@symbolic_dispatch(cls = Series)
def n_distinct(x):
"""Return the total number of distinct (i.e. unique) elements in an array.
Example:
>>> n_distinct(pd.Series([1,1,2,2]))
2
"""
return x.nunique()
# na_if -----------------------------------------------------------------------
@symbolic_dispatch(cls = Series)
def na_if(x, y):
"""Return a array like x, but with values in y replaced by NAs.
Examples:
>>> na_if(pd.Series([1,2,3]), [1,3])
0 NaN
1 2.0
2 NaN
dtype: float64
"""
y = [y] if not np.ndim(y) else y
tmp_x = x.copy(deep = True)
tmp_x[x.isin(y)] = np.nan
return tmp_x
@symbolic_dispatch(cls = Series)
def near(x):
"""TODO: Not Implemented"""
NotImplementedError("near not implemented")
@symbolic_dispatch(cls = Series)
def nth(x):
"""TODO: Not Implemented"""
NotImplementedError("nth not implemented")
@symbolic_dispatch(cls = Series)
def first(x):
"""TODO: Not Implemented"""
NotImplementedError("first not implemented")
@symbolic_dispatch(cls = Series)
def last(x):
"""TODO: Not Implemented"""
NotImplementedError("last not implemented")