forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoin.pyx
1006 lines (834 loc) · 30.8 KB
/
join.pyx
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import cython
from cython import Py_ssize_t
import numpy as np
cimport numpy as cnp
from numpy cimport (ndarray,
int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t,
uint32_t, uint64_t, float32_t, float64_t)
cnp.import_array()
from pandas._libs.algos import groupsort_indexer, ensure_platform_int
from pandas.core.algorithms import take_nd
def inner_join(ndarray[int64_t] left, ndarray[int64_t] right,
Py_ssize_t max_groups):
cdef:
Py_ssize_t i, j, k, count = 0
ndarray[int64_t] left_count, right_count, left_sorter, right_sorter
ndarray[int64_t] left_indexer, right_indexer
int64_t lc, rc
# NA group in location 0
left_sorter, left_count = groupsort_indexer(left, max_groups)
right_sorter, right_count = groupsort_indexer(right, max_groups)
# First pass, determine size of result set, do not use the NA group
for i in range(1, max_groups + 1):
lc = left_count[i]
rc = right_count[i]
if rc > 0 and lc > 0:
count += lc * rc
# group 0 is the NA group
cdef:
Py_ssize_t loc, left_pos = 0, right_pos = 0, position = 0
Py_ssize_t offset
# exclude the NA group
left_pos = left_count[0]
right_pos = right_count[0]
left_indexer = np.empty(count, dtype=np.int64)
right_indexer = np.empty(count, dtype=np.int64)
for i in range(1, max_groups + 1):
lc = left_count[i]
rc = right_count[i]
if rc > 0 and lc > 0:
for j in range(lc):
offset = position + j * rc
for k in range(rc):
left_indexer[offset + k] = left_pos + j
right_indexer[offset + k] = right_pos + k
position += lc * rc
left_pos += lc
right_pos += rc
return (_get_result_indexer(left_sorter, left_indexer),
_get_result_indexer(right_sorter, right_indexer))
def left_outer_join(ndarray[int64_t] left, ndarray[int64_t] right,
Py_ssize_t max_groups, sort=True):
cdef:
Py_ssize_t i, j, k, count = 0
ndarray[int64_t] left_count, right_count
ndarray left_sorter, right_sorter, rev
ndarray[int64_t] left_indexer, right_indexer
int64_t lc, rc
# NA group in location 0
left_sorter, left_count = groupsort_indexer(left, max_groups)
right_sorter, right_count = groupsort_indexer(right, max_groups)
# First pass, determine size of result set, do not use the NA group
for i in range(1, max_groups + 1):
if right_count[i] > 0:
count += left_count[i] * right_count[i]
else:
count += left_count[i]
# group 0 is the NA group
cdef:
Py_ssize_t loc, left_pos = 0, right_pos = 0, position = 0
Py_ssize_t offset
# exclude the NA group
left_pos = left_count[0]
right_pos = right_count[0]
left_indexer = np.empty(count, dtype=np.int64)
right_indexer = np.empty(count, dtype=np.int64)
for i in range(1, max_groups + 1):
lc = left_count[i]
rc = right_count[i]
if rc == 0:
for j in range(lc):
left_indexer[position + j] = left_pos + j
right_indexer[position + j] = -1
position += lc
else:
for j in range(lc):
offset = position + j * rc
for k in range(rc):
left_indexer[offset + k] = left_pos + j
right_indexer[offset + k] = right_pos + k
position += lc * rc
left_pos += lc
right_pos += rc
left_indexer = _get_result_indexer(left_sorter, left_indexer)
right_indexer = _get_result_indexer(right_sorter, right_indexer)
if not sort: # if not asked to sort, revert to original order
if len(left) == len(left_indexer):
# no multiple matches for any row on the left
# this is a short-cut to avoid groupsort_indexer
# otherwise, the `else` path also works in this case
left_sorter = ensure_platform_int(left_sorter)
rev = np.empty(len(left), dtype=np.intp)
rev.put(left_sorter, np.arange(len(left)))
else:
rev, _ = groupsort_indexer(left_indexer, len(left))
rev = ensure_platform_int(rev)
right_indexer = right_indexer.take(rev)
left_indexer = left_indexer.take(rev)
return left_indexer, right_indexer
def full_outer_join(ndarray[int64_t] left, ndarray[int64_t] right,
Py_ssize_t max_groups):
cdef:
Py_ssize_t i, j, k, count = 0
ndarray[int64_t] left_count, right_count, left_sorter, right_sorter
ndarray[int64_t] left_indexer, right_indexer
int64_t lc, rc
# NA group in location 0
left_sorter, left_count = groupsort_indexer(left, max_groups)
right_sorter, right_count = groupsort_indexer(right, max_groups)
# First pass, determine size of result set, do not use the NA group
for i in range(1, max_groups + 1):
lc = left_count[i]
rc = right_count[i]
if rc > 0 and lc > 0:
count += lc * rc
else:
count += lc + rc
# group 0 is the NA group
cdef:
int64_t left_pos = 0, right_pos = 0
Py_ssize_t offset, position = 0
# exclude the NA group
left_pos = left_count[0]
right_pos = right_count[0]
left_indexer = np.empty(count, dtype=np.int64)
right_indexer = np.empty(count, dtype=np.int64)
for i in range(1, max_groups + 1):
lc = left_count[i]
rc = right_count[i]
if rc == 0:
for j in range(lc):
left_indexer[position + j] = left_pos + j
right_indexer[position + j] = -1
position += lc
elif lc == 0:
for j in range(rc):
left_indexer[position + j] = -1
right_indexer[position + j] = right_pos + j
position += rc
else:
for j in range(lc):
offset = position + j * rc
for k in range(rc):
left_indexer[offset + k] = left_pos + j
right_indexer[offset + k] = right_pos + k
position += lc * rc
left_pos += lc
right_pos += rc
return (_get_result_indexer(left_sorter, left_indexer),
_get_result_indexer(right_sorter, right_indexer))
def _get_result_indexer(sorter, indexer):
if len(sorter) > 0:
res = take_nd(sorter, indexer, fill_value=-1)
else:
# length-0 case
res = np.empty(len(indexer), dtype=np.int64)
res[:] = -1
return res
def ffill_indexer(ndarray[int64_t] indexer):
cdef:
Py_ssize_t i, n = len(indexer)
ndarray[int64_t] result
int64_t val, last_obs
result = np.empty(n, dtype=np.int64)
last_obs = -1
for i in range(n):
val = indexer[i]
if val == -1:
result[i] = last_obs
else:
result[i] = val
last_obs = val
return result
# ----------------------------------------------------------------------
# left_join_indexer, inner_join_indexer, outer_join_indexer
# ----------------------------------------------------------------------
ctypedef fused join_t:
float64_t
float32_t
object
int32_t
int64_t
uint64_t
# Joins on ordered, unique indices
# right might contain non-unique values
@cython.wraparound(False)
@cython.boundscheck(False)
def left_join_indexer_unique(ndarray[join_t] left, ndarray[join_t] right):
cdef:
Py_ssize_t i, j, nleft, nright
ndarray[int64_t] indexer
join_t lval, rval
i = 0
j = 0
nleft = len(left)
nright = len(right)
indexer = np.empty(nleft, dtype=np.int64)
while True:
if i == nleft:
break
if j == nright:
indexer[i] = -1
i += 1
continue
rval = right[j]
while i < nleft - 1 and left[i] == rval:
indexer[i] = j
i += 1
if left[i] == right[j]:
indexer[i] = j
i += 1
while i < nleft - 1 and left[i] == rval:
indexer[i] = j
i += 1
j += 1
elif left[i] > rval:
indexer[i] = -1
j += 1
else:
indexer[i] = -1
i += 1
return indexer
left_join_indexer_unique_float64 = left_join_indexer_unique["float64_t"]
left_join_indexer_unique_float32 = left_join_indexer_unique["float32_t"]
left_join_indexer_unique_object = left_join_indexer_unique["object"]
left_join_indexer_unique_int32 = left_join_indexer_unique["int32_t"]
left_join_indexer_unique_int64 = left_join_indexer_unique["int64_t"]
left_join_indexer_unique_uint64 = left_join_indexer_unique["uint64_t"]
@cython.wraparound(False)
@cython.boundscheck(False)
def left_join_indexer(ndarray[join_t] left, ndarray[join_t] right):
"""
Two-pass algorithm for monotonic indexes. Handles many-to-one merges
"""
cdef:
Py_ssize_t i, j, k, nright, nleft, count
join_t lval, rval
ndarray[int64_t] lindexer, rindexer
ndarray[join_t] result
nleft = len(left)
nright = len(right)
i = 0
j = 0
count = 0
if nleft > 0:
while i < nleft:
if j == nright:
count += nleft - i
break
lval = left[i]
rval = right[j]
if lval == rval:
count += 1
if i < nleft - 1:
if j < nright - 1 and right[j + 1] == rval:
j += 1
else:
i += 1
if left[i] != rval:
j += 1
elif j < nright - 1:
j += 1
if lval != right[j]:
i += 1
else:
# end of the road
break
elif lval < rval:
count += 1
i += 1
else:
j += 1
# do it again now that result size is known
lindexer = np.empty(count, dtype=np.int64)
rindexer = np.empty(count, dtype=np.int64)
result = np.empty(count, dtype=left.dtype)
i = 0
j = 0
count = 0
if nleft > 0:
while i < nleft:
if j == nright:
while i < nleft:
lindexer[count] = i
rindexer[count] = -1
result[count] = left[i]
i += 1
count += 1
break
lval = left[i]
rval = right[j]
if lval == rval:
lindexer[count] = i
rindexer[count] = j
result[count] = lval
count += 1
if i < nleft - 1:
if j < nright - 1 and right[j + 1] == rval:
j += 1
else:
i += 1
if left[i] != rval:
j += 1
elif j < nright - 1:
j += 1
if lval != right[j]:
i += 1
else:
# end of the road
break
elif lval < rval:
lindexer[count] = i
rindexer[count] = -1
result[count] = left[i]
count += 1
i += 1
else:
j += 1
return result, lindexer, rindexer
left_join_indexer_float64 = left_join_indexer["float64_t"]
left_join_indexer_float32 = left_join_indexer["float32_t"]
left_join_indexer_object = left_join_indexer["object"]
left_join_indexer_int32 = left_join_indexer["int32_t"]
left_join_indexer_int64 = left_join_indexer["int64_t"]
left_join_indexer_uint64 = left_join_indexer["uint64_t"]
@cython.wraparound(False)
@cython.boundscheck(False)
def inner_join_indexer(ndarray[join_t] left, ndarray[join_t] right):
"""
Two-pass algorithm for monotonic indexes. Handles many-to-one merges
"""
cdef:
Py_ssize_t i, j, k, nright, nleft, count
join_t lval, rval
ndarray[int64_t] lindexer, rindexer
ndarray[join_t] result
nleft = len(left)
nright = len(right)
i = 0
j = 0
count = 0
if nleft > 0 and nright > 0:
while True:
if i == nleft:
break
if j == nright:
break
lval = left[i]
rval = right[j]
if lval == rval:
count += 1
if i < nleft - 1:
if j < nright - 1 and right[j + 1] == rval:
j += 1
else:
i += 1
if left[i] != rval:
j += 1
elif j < nright - 1:
j += 1
if lval != right[j]:
i += 1
else:
# end of the road
break
elif lval < rval:
i += 1
else:
j += 1
# do it again now that result size is known
lindexer = np.empty(count, dtype=np.int64)
rindexer = np.empty(count, dtype=np.int64)
result = np.empty(count, dtype=left.dtype)
i = 0
j = 0
count = 0
if nleft > 0 and nright > 0:
while True:
if i == nleft:
break
if j == nright:
break
lval = left[i]
rval = right[j]
if lval == rval:
lindexer[count] = i
rindexer[count] = j
result[count] = rval
count += 1
if i < nleft - 1:
if j < nright - 1 and right[j + 1] == rval:
j += 1
else:
i += 1
if left[i] != rval:
j += 1
elif j < nright - 1:
j += 1
if lval != right[j]:
i += 1
else:
# end of the road
break
elif lval < rval:
i += 1
else:
j += 1
return result, lindexer, rindexer
inner_join_indexer_float64 = inner_join_indexer["float64_t"]
inner_join_indexer_float32 = inner_join_indexer["float32_t"]
inner_join_indexer_object = inner_join_indexer["object"]
inner_join_indexer_int32 = inner_join_indexer["int32_t"]
inner_join_indexer_int64 = inner_join_indexer["int64_t"]
inner_join_indexer_uint64 = inner_join_indexer["uint64_t"]
@cython.wraparound(False)
@cython.boundscheck(False)
def outer_join_indexer(ndarray[join_t] left, ndarray[join_t] right):
cdef:
Py_ssize_t i, j, nright, nleft, count
join_t lval, rval
ndarray[int64_t] lindexer, rindexer
ndarray[join_t] result
nleft = len(left)
nright = len(right)
i = 0
j = 0
count = 0
if nleft == 0:
count = nright
elif nright == 0:
count = nleft
else:
while True:
if i == nleft:
count += nright - j
break
if j == nright:
count += nleft - i
break
lval = left[i]
rval = right[j]
if lval == rval:
count += 1
if i < nleft - 1:
if j < nright - 1 and right[j + 1] == rval:
j += 1
else:
i += 1
if left[i] != rval:
j += 1
elif j < nright - 1:
j += 1
if lval != right[j]:
i += 1
else:
# end of the road
break
elif lval < rval:
count += 1
i += 1
else:
count += 1
j += 1
lindexer = np.empty(count, dtype=np.int64)
rindexer = np.empty(count, dtype=np.int64)
result = np.empty(count, dtype=left.dtype)
# do it again, but populate the indexers / result
i = 0
j = 0
count = 0
if nleft == 0:
for j in range(nright):
lindexer[j] = -1
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
else:
while True:
if i == nleft:
while j < nright:
lindexer[count] = -1
rindexer[count] = j
result[count] = right[j]
count += 1
j += 1
break
if j == nright:
while i < nleft:
lindexer[count] = i
rindexer[count] = -1
result[count] = left[i]
count += 1
i += 1
break
lval = left[i]
rval = right[j]
if lval == rval:
lindexer[count] = i
rindexer[count] = j
result[count] = lval
count += 1
if i < nleft - 1:
if j < nright - 1 and right[j + 1] == rval:
j += 1
else:
i += 1
if left[i] != rval:
j += 1
elif j < nright - 1:
j += 1
if lval != right[j]:
i += 1
else:
# end of the road
break
elif lval < rval:
lindexer[count] = i
rindexer[count] = -1
result[count] = lval
count += 1
i += 1
else:
lindexer[count] = -1
rindexer[count] = j
result[count] = rval
count += 1
j += 1
return result, lindexer, rindexer
outer_join_indexer_float64 = outer_join_indexer["float64_t"]
outer_join_indexer_float32 = outer_join_indexer["float32_t"]
outer_join_indexer_object = outer_join_indexer["object"]
outer_join_indexer_int32 = outer_join_indexer["int32_t"]
outer_join_indexer_int64 = outer_join_indexer["int64_t"]
outer_join_indexer_uint64 = outer_join_indexer["uint64_t"]
# ----------------------------------------------------------------------
# asof_join_by
# ----------------------------------------------------------------------
from pandas._libs.hashtable cimport (
HashTable, PyObjectHashTable, UInt64HashTable, Int64HashTable)
ctypedef fused asof_t:
uint8_t
uint16_t
uint32_t
uint64_t
int8_t
int16_t
int32_t
int64_t
float
float64_t
ctypedef fused by_t:
object
int64_t
uint64_t
def asof_join_backward_on_X_by_Y(ndarray[asof_t] left_values,
ndarray[asof_t] right_values,
ndarray[by_t] left_by_values,
ndarray[by_t] 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
asof_t tolerance_ = 0
asof_t diff = 0
HashTable hash_table
by_t 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)
if by_t is object:
hash_table = PyObjectHashTable(right_size)
elif by_t is int64_t:
hash_table = Int64HashTable(right_size)
elif by_t is uint64_t:
hash_table = UInt64HashTable(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_X_by_Y(ndarray[asof_t] left_values,
ndarray[asof_t] right_values,
ndarray[by_t] left_by_values,
ndarray[by_t] 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
asof_t tolerance_ = 0
asof_t diff = 0
HashTable hash_table
by_t 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)
if by_t is object:
hash_table = PyObjectHashTable(right_size)
elif by_t is int64_t:
hash_table = Int64HashTable(right_size)
elif by_t is uint64_t:
hash_table = UInt64HashTable(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_X_by_Y(ndarray[asof_t] left_values,
ndarray[asof_t] right_values,
ndarray[by_t] left_by_values,
ndarray[by_t] 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
asof_t 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_X_by_Y(left_values,
right_values,
left_by_values,
right_by_values,
allow_exact_matches,
tolerance)
fli, fri = asof_join_forward_on_X_by_Y(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
# ----------------------------------------------------------------------
# asof_join
# ----------------------------------------------------------------------
def asof_join_backward(ndarray[asof_t] left_values,
ndarray[asof_t] 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
asof_t tolerance_ = 0
asof_t 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(ndarray[asof_t] left_values,
ndarray[asof_t] 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
asof_t tolerance_ = 0
asof_t 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(ndarray[asof_t] left_values,
ndarray[asof_t] 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
asof_t 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(left_values, right_values,
allow_exact_matches, tolerance)
fli, fri = asof_join_forward(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]]