forked from pandas-dev/pandas
/
join_func_helper.pxi.in
373 lines (294 loc) · 12.5 KB
/
join_func_helper.pxi.in
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
365
366
367
368
369
370
371
372
373
# cython: boundscheck=False, wraparound=False
"""
Template for each `dtype` helper function for hashtable
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
#----------------------------------------------------------------------
# asof_join_by
#----------------------------------------------------------------------
{{py:
# table_type, by_dtype
by_dtypes = [('PyObjectHashTable', 'object'), ('Int64HashTable', 'int64_t'),
('UInt64HashTable', 'uint64_t')]
# on_dtype
on_dtypes = ['uint8_t', 'uint16_t', 'uint32_t', 'uint64_t',
'int8_t', 'int16_t', 'int32_t', 'int64_t',
'float', 'double']
}}
from hashtable cimport *
{{for table_type, by_dtype in by_dtypes}}
{{for on_dtype in on_dtypes}}
def asof_join_backward_{{on_dtype}}_by_{{by_dtype}}(
ndarray[{{on_dtype}}] left_values,
ndarray[{{on_dtype}}] right_values,
ndarray[{{by_dtype}}] left_by_values,
ndarray[{{by_dtype}}] right_by_values,
bint allow_exact_matches=1,
tolerance=None):
cdef:
Py_ssize_t left_pos, right_pos, left_size, right_size, found_right_pos
ndarray[int64_t] left_indexer, right_indexer
bint has_tolerance = 0
{{on_dtype}} tolerance_ = 0
{{on_dtype}} diff = 0
{{table_type}} hash_table
{{by_dtype}} by_value
# if we are using tolerance, set our objects
if tolerance is not None:
has_tolerance = 1
tolerance_ = tolerance
left_size = len(left_values)
right_size = len(right_values)
left_indexer = np.empty(left_size, dtype=np.int64)
right_indexer = np.empty(left_size, dtype=np.int64)
hash_table = {{table_type}}(right_size)
right_pos = 0
for left_pos in range(left_size):
# restart right_pos if it went negative in a previous iteration
if right_pos < 0:
right_pos = 0
# find last position in right whose value is less than left's
if allow_exact_matches:
while right_pos < right_size and\
right_values[right_pos] <= left_values[left_pos]:
hash_table.set_item(right_by_values[right_pos], right_pos)
right_pos += 1
else:
while right_pos < right_size and\
right_values[right_pos] < left_values[left_pos]:
hash_table.set_item(right_by_values[right_pos], right_pos)
right_pos += 1
right_pos -= 1
# save positions as the desired index
by_value = left_by_values[left_pos]
found_right_pos = hash_table.get_item(by_value)\
if by_value in hash_table else -1
left_indexer[left_pos] = left_pos
right_indexer[left_pos] = found_right_pos
# if needed, verify that tolerance is met
if has_tolerance and found_right_pos != -1:
diff = left_values[left_pos] - right_values[found_right_pos]
if diff > tolerance_:
right_indexer[left_pos] = -1
return left_indexer, right_indexer
def asof_join_forward_{{on_dtype}}_by_{{by_dtype}}(
ndarray[{{on_dtype}}] left_values,
ndarray[{{on_dtype}}] right_values,
ndarray[{{by_dtype}}] left_by_values,
ndarray[{{by_dtype}}] right_by_values,
bint allow_exact_matches=1,
tolerance=None):
cdef:
Py_ssize_t left_pos, right_pos, left_size, right_size, found_right_pos
ndarray[int64_t] left_indexer, right_indexer
bint has_tolerance = 0
{{on_dtype}} tolerance_ = 0
{{on_dtype}} diff = 0
{{table_type}} hash_table
{{by_dtype}} by_value
# if we are using tolerance, set our objects
if tolerance is not None:
has_tolerance = 1
tolerance_ = tolerance
left_size = len(left_values)
right_size = len(right_values)
left_indexer = np.empty(left_size, dtype=np.int64)
right_indexer = np.empty(left_size, dtype=np.int64)
hash_table = {{table_type}}(right_size)
right_pos = right_size - 1
for left_pos in range(left_size - 1, -1, -1):
# restart right_pos if it went over in a previous iteration
if right_pos == right_size:
right_pos = right_size - 1
# find first position in right whose value is greater than left's
if allow_exact_matches:
while right_pos >= 0 and\
right_values[right_pos] >= left_values[left_pos]:
hash_table.set_item(right_by_values[right_pos], right_pos)
right_pos -= 1
else:
while right_pos >= 0 and\
right_values[right_pos] > left_values[left_pos]:
hash_table.set_item(right_by_values[right_pos], right_pos)
right_pos -= 1
right_pos += 1
# save positions as the desired index
by_value = left_by_values[left_pos]
found_right_pos = hash_table.get_item(by_value)\
if by_value in hash_table else -1
left_indexer[left_pos] = left_pos
right_indexer[left_pos] = found_right_pos
# if needed, verify that tolerance is met
if has_tolerance and found_right_pos != -1:
diff = right_values[found_right_pos] - left_values[left_pos]
if diff > tolerance_:
right_indexer[left_pos] = -1
return left_indexer, right_indexer
def asof_join_nearest_{{on_dtype}}_by_{{by_dtype}}(
ndarray[{{on_dtype}}] left_values,
ndarray[{{on_dtype}}] right_values,
ndarray[{{by_dtype}}] left_by_values,
ndarray[{{by_dtype}}] right_by_values,
bint allow_exact_matches=1,
tolerance=None):
cdef:
Py_ssize_t left_size, right_size, i
ndarray[int64_t] left_indexer, right_indexer, bli, bri, fli, fri
{{on_dtype}} bdiff, fdiff
left_size = len(left_values)
right_size = len(right_values)
left_indexer = np.empty(left_size, dtype=np.int64)
right_indexer = np.empty(left_size, dtype=np.int64)
# search both forward and backward
bli, bri =\
asof_join_backward_{{on_dtype}}_by_{{by_dtype}}(left_values,
right_values,
left_by_values,
right_by_values,
allow_exact_matches,
tolerance)
fli, fri =\
asof_join_forward_{{on_dtype}}_by_{{by_dtype}}(left_values,
right_values,
left_by_values,
right_by_values,
allow_exact_matches,
tolerance)
for i in range(len(bri)):
# choose timestamp from right with smaller difference
if bri[i] != -1 and fri[i] != -1:
bdiff = left_values[bli[i]] - right_values[bri[i]]
fdiff = right_values[fri[i]] - left_values[fli[i]]
right_indexer[i] = bri[i] if bdiff <= fdiff else fri[i]
else:
right_indexer[i] = bri[i] if bri[i] != -1 else fri[i]
left_indexer[i] = bli[i]
return left_indexer, right_indexer
{{endfor}}
{{endfor}}
#----------------------------------------------------------------------
# asof_join
#----------------------------------------------------------------------
{{py:
# on_dtype
dtypes = ['uint8_t', 'uint16_t', 'uint32_t', 'uint64_t',
'int8_t', 'int16_t', 'int32_t', 'int64_t',
'float', 'double']
}}
{{for on_dtype in dtypes}}
def asof_join_backward_{{on_dtype}}(
ndarray[{{on_dtype}}] left_values,
ndarray[{{on_dtype}}] right_values,
bint allow_exact_matches=1,
tolerance=None):
cdef:
Py_ssize_t left_pos, right_pos, left_size, right_size
ndarray[int64_t] left_indexer, right_indexer
bint has_tolerance = 0
{{on_dtype}} tolerance_ = 0
{{on_dtype}} diff = 0
# if we are using tolerance, set our objects
if tolerance is not None:
has_tolerance = 1
tolerance_ = tolerance
left_size = len(left_values)
right_size = len(right_values)
left_indexer = np.empty(left_size, dtype=np.int64)
right_indexer = np.empty(left_size, dtype=np.int64)
right_pos = 0
for left_pos in range(left_size):
# restart right_pos if it went negative in a previous iteration
if right_pos < 0:
right_pos = 0
# find last position in right whose value is less than left's
if allow_exact_matches:
while right_pos < right_size and\
right_values[right_pos] <= left_values[left_pos]:
right_pos += 1
else:
while right_pos < right_size and\
right_values[right_pos] < left_values[left_pos]:
right_pos += 1
right_pos -= 1
# save positions as the desired index
left_indexer[left_pos] = left_pos
right_indexer[left_pos] = right_pos
# if needed, verify that tolerance is met
if has_tolerance and right_pos != -1:
diff = left_values[left_pos] - right_values[right_pos]
if diff > tolerance_:
right_indexer[left_pos] = -1
return left_indexer, right_indexer
def asof_join_forward_{{on_dtype}}(
ndarray[{{on_dtype}}] left_values,
ndarray[{{on_dtype}}] right_values,
bint allow_exact_matches=1,
tolerance=None):
cdef:
Py_ssize_t left_pos, right_pos, left_size, right_size
ndarray[int64_t] left_indexer, right_indexer
bint has_tolerance = 0
{{on_dtype}} tolerance_ = 0
{{on_dtype}} diff = 0
# if we are using tolerance, set our objects
if tolerance is not None:
has_tolerance = 1
tolerance_ = tolerance
left_size = len(left_values)
right_size = len(right_values)
left_indexer = np.empty(left_size, dtype=np.int64)
right_indexer = np.empty(left_size, dtype=np.int64)
right_pos = right_size - 1
for left_pos in range(left_size - 1, -1, -1):
# restart right_pos if it went over in a previous iteration
if right_pos == right_size:
right_pos = right_size - 1
# find first position in right whose value is greater than left's
if allow_exact_matches:
while right_pos >= 0 and\
right_values[right_pos] >= left_values[left_pos]:
right_pos -= 1
else:
while right_pos >= 0 and\
right_values[right_pos] > left_values[left_pos]:
right_pos -= 1
right_pos += 1
# save positions as the desired index
left_indexer[left_pos] = left_pos
right_indexer[left_pos] = right_pos\
if right_pos != right_size else -1
# if needed, verify that tolerance is met
if has_tolerance and right_pos != right_size:
diff = right_values[right_pos] - left_values[left_pos]
if diff > tolerance_:
right_indexer[left_pos] = -1
return left_indexer, right_indexer
def asof_join_nearest_{{on_dtype}}(
ndarray[{{on_dtype}}] left_values,
ndarray[{{on_dtype}}] right_values,
bint allow_exact_matches=1,
tolerance=None):
cdef:
Py_ssize_t left_size, right_size, i
ndarray[int64_t] left_indexer, right_indexer, bli, bri, fli, fri
{{on_dtype}} bdiff, fdiff
left_size = len(left_values)
right_size = len(right_values)
left_indexer = np.empty(left_size, dtype=np.int64)
right_indexer = np.empty(left_size, dtype=np.int64)
# search both forward and backward
bli, bri = asof_join_backward_{{on_dtype}}(left_values, right_values,
allow_exact_matches, tolerance)
fli, fri = asof_join_forward_{{on_dtype}}(left_values, right_values,
allow_exact_matches, tolerance)
for i in range(len(bri)):
# choose timestamp from right with smaller difference
if bri[i] != -1 and fri[i] != -1:
bdiff = left_values[bli[i]] - right_values[bri[i]]
fdiff = right_values[fri[i]] - left_values[fli[i]]
right_indexer[i] = bri[i] if bdiff <= fdiff else fri[i]
else:
right_indexer[i] = bri[i] if bri[i] != -1 else fri[i]
left_indexer[i] = bli[i]
return left_indexer, right_indexer
{{endfor}}