join.pyx 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. import cython
  2. from cython import Py_ssize_t
  3. import numpy as np
  4. cimport numpy as cnp
  5. from numpy cimport (
  6. float32_t,
  7. float64_t,
  8. int8_t,
  9. int16_t,
  10. int32_t,
  11. int64_t,
  12. intp_t,
  13. ndarray,
  14. uint8_t,
  15. uint16_t,
  16. uint32_t,
  17. uint64_t,
  18. )
  19. cnp.import_array()
  20. from pandas._libs.algos import groupsort_indexer
  21. @cython.wraparound(False)
  22. @cython.boundscheck(False)
  23. def inner_join(const intp_t[:] left, const intp_t[:] right,
  24. Py_ssize_t max_groups):
  25. cdef:
  26. Py_ssize_t i, j, k, count = 0
  27. intp_t[::1] left_sorter, right_sorter
  28. intp_t[::1] left_count, right_count
  29. intp_t[::1] left_indexer, right_indexer
  30. intp_t lc, rc
  31. Py_ssize_t left_pos = 0, right_pos = 0, position = 0
  32. Py_ssize_t offset
  33. left_sorter, left_count = groupsort_indexer(left, max_groups)
  34. right_sorter, right_count = groupsort_indexer(right, max_groups)
  35. with nogil:
  36. # First pass, determine size of result set, do not use the NA group
  37. for i in range(1, max_groups + 1):
  38. lc = left_count[i]
  39. rc = right_count[i]
  40. if rc > 0 and lc > 0:
  41. count += lc * rc
  42. left_indexer = np.empty(count, dtype=np.intp)
  43. right_indexer = np.empty(count, dtype=np.intp)
  44. with nogil:
  45. # exclude the NA group
  46. left_pos = left_count[0]
  47. right_pos = right_count[0]
  48. for i in range(1, max_groups + 1):
  49. lc = left_count[i]
  50. rc = right_count[i]
  51. if rc > 0 and lc > 0:
  52. for j in range(lc):
  53. offset = position + j * rc
  54. for k in range(rc):
  55. left_indexer[offset + k] = left_pos + j
  56. right_indexer[offset + k] = right_pos + k
  57. position += lc * rc
  58. left_pos += lc
  59. right_pos += rc
  60. # Will overwrite left/right indexer with the result
  61. _get_result_indexer(left_sorter, left_indexer)
  62. _get_result_indexer(right_sorter, right_indexer)
  63. return np.asarray(left_indexer), np.asarray(right_indexer)
  64. @cython.wraparound(False)
  65. @cython.boundscheck(False)
  66. def left_outer_join(const intp_t[:] left, const intp_t[:] right,
  67. Py_ssize_t max_groups, bint sort=True):
  68. cdef:
  69. Py_ssize_t i, j, k, count = 0
  70. ndarray[intp_t] rev
  71. intp_t[::1] left_count, right_count
  72. intp_t[::1] left_sorter, right_sorter
  73. intp_t[::1] left_indexer, right_indexer
  74. intp_t lc, rc
  75. Py_ssize_t left_pos = 0, right_pos = 0, position = 0
  76. Py_ssize_t offset
  77. left_sorter, left_count = groupsort_indexer(left, max_groups)
  78. right_sorter, right_count = groupsort_indexer(right, max_groups)
  79. with nogil:
  80. # First pass, determine size of result set, do not use the NA group
  81. for i in range(1, max_groups + 1):
  82. if right_count[i] > 0:
  83. count += left_count[i] * right_count[i]
  84. else:
  85. count += left_count[i]
  86. left_indexer = np.empty(count, dtype=np.intp)
  87. right_indexer = np.empty(count, dtype=np.intp)
  88. with nogil:
  89. # exclude the NA group
  90. left_pos = left_count[0]
  91. right_pos = right_count[0]
  92. for i in range(1, max_groups + 1):
  93. lc = left_count[i]
  94. rc = right_count[i]
  95. if rc == 0:
  96. for j in range(lc):
  97. left_indexer[position + j] = left_pos + j
  98. right_indexer[position + j] = -1
  99. position += lc
  100. else:
  101. for j in range(lc):
  102. offset = position + j * rc
  103. for k in range(rc):
  104. left_indexer[offset + k] = left_pos + j
  105. right_indexer[offset + k] = right_pos + k
  106. position += lc * rc
  107. left_pos += lc
  108. right_pos += rc
  109. # Will overwrite left/right indexer with the result
  110. _get_result_indexer(left_sorter, left_indexer)
  111. _get_result_indexer(right_sorter, right_indexer)
  112. if not sort: # if not asked to sort, revert to original order
  113. if len(left) == len(left_indexer):
  114. # no multiple matches for any row on the left
  115. # this is a short-cut to avoid groupsort_indexer
  116. # otherwise, the `else` path also works in this case
  117. rev = np.empty(len(left), dtype=np.intp)
  118. rev.put(np.asarray(left_sorter), np.arange(len(left)))
  119. else:
  120. rev, _ = groupsort_indexer(left_indexer, len(left))
  121. return np.asarray(left_indexer).take(rev), np.asarray(right_indexer).take(rev)
  122. else:
  123. return np.asarray(left_indexer), np.asarray(right_indexer)
  124. @cython.wraparound(False)
  125. @cython.boundscheck(False)
  126. def full_outer_join(const intp_t[:] left, const intp_t[:] right,
  127. Py_ssize_t max_groups):
  128. cdef:
  129. Py_ssize_t i, j, k, count = 0
  130. intp_t[::1] left_sorter, right_sorter
  131. intp_t[::1] left_count, right_count
  132. intp_t[::1] left_indexer, right_indexer
  133. intp_t lc, rc
  134. intp_t left_pos = 0, right_pos = 0
  135. Py_ssize_t offset, position = 0
  136. left_sorter, left_count = groupsort_indexer(left, max_groups)
  137. right_sorter, right_count = groupsort_indexer(right, max_groups)
  138. with nogil:
  139. # First pass, determine size of result set, do not use the NA group
  140. for i in range(1, max_groups + 1):
  141. lc = left_count[i]
  142. rc = right_count[i]
  143. if rc > 0 and lc > 0:
  144. count += lc * rc
  145. else:
  146. count += lc + rc
  147. left_indexer = np.empty(count, dtype=np.intp)
  148. right_indexer = np.empty(count, dtype=np.intp)
  149. with nogil:
  150. # exclude the NA group
  151. left_pos = left_count[0]
  152. right_pos = right_count[0]
  153. for i in range(1, max_groups + 1):
  154. lc = left_count[i]
  155. rc = right_count[i]
  156. if rc == 0:
  157. for j in range(lc):
  158. left_indexer[position + j] = left_pos + j
  159. right_indexer[position + j] = -1
  160. position += lc
  161. elif lc == 0:
  162. for j in range(rc):
  163. left_indexer[position + j] = -1
  164. right_indexer[position + j] = right_pos + j
  165. position += rc
  166. else:
  167. for j in range(lc):
  168. offset = position + j * rc
  169. for k in range(rc):
  170. left_indexer[offset + k] = left_pos + j
  171. right_indexer[offset + k] = right_pos + k
  172. position += lc * rc
  173. left_pos += lc
  174. right_pos += rc
  175. # Will overwrite left/right indexer with the result
  176. _get_result_indexer(left_sorter, left_indexer)
  177. _get_result_indexer(right_sorter, right_indexer)
  178. return np.asarray(left_indexer), np.asarray(right_indexer)
  179. @cython.wraparound(False)
  180. @cython.boundscheck(False)
  181. cdef void _get_result_indexer(intp_t[::1] sorter, intp_t[::1] indexer) nogil:
  182. """NOTE: overwrites indexer with the result to avoid allocating another array"""
  183. cdef:
  184. Py_ssize_t i, n, idx
  185. if len(sorter) > 0:
  186. # cython-only equivalent to
  187. # `res = algos.take_nd(sorter, indexer, fill_value=-1)`
  188. n = indexer.shape[0]
  189. for i in range(n):
  190. idx = indexer[i]
  191. if idx == -1:
  192. indexer[i] = -1
  193. else:
  194. indexer[i] = sorter[idx]
  195. else:
  196. # length-0 case
  197. indexer[:] = -1
  198. def ffill_indexer(const intp_t[:] indexer) -> np.ndarray:
  199. cdef:
  200. Py_ssize_t i, n = len(indexer)
  201. ndarray[intp_t] result
  202. intp_t val, last_obs
  203. result = np.empty(n, dtype=np.intp)
  204. last_obs = -1
  205. for i in range(n):
  206. val = indexer[i]
  207. if val == -1:
  208. result[i] = last_obs
  209. else:
  210. result[i] = val
  211. last_obs = val
  212. return result
  213. # ----------------------------------------------------------------------
  214. # left_join_indexer, inner_join_indexer, outer_join_indexer
  215. # ----------------------------------------------------------------------
  216. ctypedef fused join_t:
  217. float64_t
  218. float32_t
  219. object
  220. int8_t
  221. int16_t
  222. int32_t
  223. int64_t
  224. uint64_t
  225. # Joins on ordered, unique indices
  226. # right might contain non-unique values
  227. @cython.wraparound(False)
  228. @cython.boundscheck(False)
  229. def left_join_indexer_unique(ndarray[join_t] left, ndarray[join_t] right):
  230. cdef:
  231. Py_ssize_t i, j, nleft, nright
  232. ndarray[intp_t] indexer
  233. join_t lval, rval
  234. i = 0
  235. j = 0
  236. nleft = len(left)
  237. nright = len(right)
  238. indexer = np.empty(nleft, dtype=np.intp)
  239. while True:
  240. if i == nleft:
  241. break
  242. if j == nright:
  243. indexer[i] = -1
  244. i += 1
  245. continue
  246. rval = right[j]
  247. while i < nleft - 1 and left[i] == rval:
  248. indexer[i] = j
  249. i += 1
  250. if left[i] == right[j]:
  251. indexer[i] = j
  252. i += 1
  253. while i < nleft - 1 and left[i] == rval:
  254. indexer[i] = j
  255. i += 1
  256. j += 1
  257. elif left[i] > rval:
  258. indexer[i] = -1
  259. j += 1
  260. else:
  261. indexer[i] = -1
  262. i += 1
  263. return indexer
  264. @cython.wraparound(False)
  265. @cython.boundscheck(False)
  266. def left_join_indexer(ndarray[join_t] left, ndarray[join_t] right):
  267. """
  268. Two-pass algorithm for monotonic indexes. Handles many-to-one merges.
  269. """
  270. cdef:
  271. Py_ssize_t i, j, k, nright, nleft, count
  272. join_t lval, rval
  273. ndarray[intp_t] lindexer, rindexer
  274. ndarray[join_t] result
  275. nleft = len(left)
  276. nright = len(right)
  277. i = 0
  278. j = 0
  279. count = 0
  280. if nleft > 0:
  281. while i < nleft:
  282. if j == nright:
  283. count += nleft - i
  284. break
  285. lval = left[i]
  286. rval = right[j]
  287. if lval == rval:
  288. count += 1
  289. if i < nleft - 1:
  290. if j < nright - 1 and right[j + 1] == rval:
  291. j += 1
  292. else:
  293. i += 1
  294. if left[i] != rval:
  295. j += 1
  296. elif j < nright - 1:
  297. j += 1
  298. if lval != right[j]:
  299. i += 1
  300. else:
  301. # end of the road
  302. break
  303. elif lval < rval:
  304. count += 1
  305. i += 1
  306. else:
  307. j += 1
  308. # do it again now that result size is known
  309. lindexer = np.empty(count, dtype=np.intp)
  310. rindexer = np.empty(count, dtype=np.intp)
  311. result = np.empty(count, dtype=left.dtype)
  312. i = 0
  313. j = 0
  314. count = 0
  315. if nleft > 0:
  316. while i < nleft:
  317. if j == nright:
  318. while i < nleft:
  319. lindexer[count] = i
  320. rindexer[count] = -1
  321. result[count] = left[i]
  322. i += 1
  323. count += 1
  324. break
  325. lval = left[i]
  326. rval = right[j]
  327. if lval == rval:
  328. lindexer[count] = i
  329. rindexer[count] = j
  330. result[count] = lval
  331. count += 1
  332. if i < nleft - 1:
  333. if j < nright - 1 and right[j + 1] == rval:
  334. j += 1
  335. else:
  336. i += 1
  337. if left[i] != rval:
  338. j += 1
  339. elif j < nright - 1:
  340. j += 1
  341. if lval != right[j]:
  342. i += 1
  343. else:
  344. # end of the road
  345. break
  346. elif lval < rval:
  347. lindexer[count] = i
  348. rindexer[count] = -1
  349. result[count] = left[i]
  350. count += 1
  351. i += 1
  352. else:
  353. j += 1
  354. return result, lindexer, rindexer
  355. @cython.wraparound(False)
  356. @cython.boundscheck(False)
  357. def inner_join_indexer(ndarray[join_t] left, ndarray[join_t] right):
  358. """
  359. Two-pass algorithm for monotonic indexes. Handles many-to-one merges.
  360. """
  361. cdef:
  362. Py_ssize_t i, j, k, nright, nleft, count
  363. join_t lval, rval
  364. ndarray[intp_t] lindexer, rindexer
  365. ndarray[join_t] result
  366. nleft = len(left)
  367. nright = len(right)
  368. i = 0
  369. j = 0
  370. count = 0
  371. if nleft > 0 and nright > 0:
  372. while True:
  373. if i == nleft:
  374. break
  375. if j == nright:
  376. break
  377. lval = left[i]
  378. rval = right[j]
  379. if lval == rval:
  380. count += 1
  381. if i < nleft - 1:
  382. if j < nright - 1 and right[j + 1] == rval:
  383. j += 1
  384. else:
  385. i += 1
  386. if left[i] != rval:
  387. j += 1
  388. elif j < nright - 1:
  389. j += 1
  390. if lval != right[j]:
  391. i += 1
  392. else:
  393. # end of the road
  394. break
  395. elif lval < rval:
  396. i += 1
  397. else:
  398. j += 1
  399. # do it again now that result size is known
  400. lindexer = np.empty(count, dtype=np.intp)
  401. rindexer = np.empty(count, dtype=np.intp)
  402. result = np.empty(count, dtype=left.dtype)
  403. i = 0
  404. j = 0
  405. count = 0
  406. if nleft > 0 and nright > 0:
  407. while True:
  408. if i == nleft:
  409. break
  410. if j == nright:
  411. break
  412. lval = left[i]
  413. rval = right[j]
  414. if lval == rval:
  415. lindexer[count] = i
  416. rindexer[count] = j
  417. result[count] = rval
  418. count += 1
  419. if i < nleft - 1:
  420. if j < nright - 1 and right[j + 1] == rval:
  421. j += 1
  422. else:
  423. i += 1
  424. if left[i] != rval:
  425. j += 1
  426. elif j < nright - 1:
  427. j += 1
  428. if lval != right[j]:
  429. i += 1
  430. else:
  431. # end of the road
  432. break
  433. elif lval < rval:
  434. i += 1
  435. else:
  436. j += 1
  437. return result, lindexer, rindexer
  438. @cython.wraparound(False)
  439. @cython.boundscheck(False)
  440. def outer_join_indexer(ndarray[join_t] left, ndarray[join_t] right):
  441. cdef:
  442. Py_ssize_t i, j, nright, nleft, count
  443. join_t lval, rval
  444. ndarray[intp_t] lindexer, rindexer
  445. ndarray[join_t] result
  446. nleft = len(left)
  447. nright = len(right)
  448. i = 0
  449. j = 0
  450. count = 0
  451. if nleft == 0:
  452. count = nright
  453. elif nright == 0:
  454. count = nleft
  455. else:
  456. while True:
  457. if i == nleft:
  458. count += nright - j
  459. break
  460. if j == nright:
  461. count += nleft - i
  462. break
  463. lval = left[i]
  464. rval = right[j]
  465. if lval == rval:
  466. count += 1
  467. if i < nleft - 1:
  468. if j < nright - 1 and right[j + 1] == rval:
  469. j += 1
  470. else:
  471. i += 1
  472. if left[i] != rval:
  473. j += 1
  474. elif j < nright - 1:
  475. j += 1
  476. if lval != right[j]:
  477. i += 1
  478. else:
  479. # end of the road
  480. break
  481. elif lval < rval:
  482. count += 1
  483. i += 1
  484. else:
  485. count += 1
  486. j += 1
  487. lindexer = np.empty(count, dtype=np.intp)
  488. rindexer = np.empty(count, dtype=np.intp)
  489. result = np.empty(count, dtype=left.dtype)
  490. # do it again, but populate the indexers / result
  491. i = 0
  492. j = 0
  493. count = 0
  494. if nleft == 0:
  495. for j in range(nright):
  496. lindexer[j] = -1
  497. rindexer[j] = j
  498. result[j] = right[j]
  499. elif nright == 0:
  500. for i in range(nleft):
  501. lindexer[i] = i
  502. rindexer[i] = -1
  503. result[i] = left[i]
  504. else:
  505. while True:
  506. if i == nleft:
  507. while j < nright:
  508. lindexer[count] = -1
  509. rindexer[count] = j
  510. result[count] = right[j]
  511. count += 1
  512. j += 1
  513. break
  514. if j == nright:
  515. while i < nleft:
  516. lindexer[count] = i
  517. rindexer[count] = -1
  518. result[count] = left[i]
  519. count += 1
  520. i += 1
  521. break
  522. lval = left[i]
  523. rval = right[j]
  524. if lval == rval:
  525. lindexer[count] = i
  526. rindexer[count] = j
  527. result[count] = lval
  528. count += 1
  529. if i < nleft - 1:
  530. if j < nright - 1 and right[j + 1] == rval:
  531. j += 1
  532. else:
  533. i += 1
  534. if left[i] != rval:
  535. j += 1
  536. elif j < nright - 1:
  537. j += 1
  538. if lval != right[j]:
  539. i += 1
  540. else:
  541. # end of the road
  542. break
  543. elif lval < rval:
  544. lindexer[count] = i
  545. rindexer[count] = -1
  546. result[count] = lval
  547. count += 1
  548. i += 1
  549. else:
  550. lindexer[count] = -1
  551. rindexer[count] = j
  552. result[count] = rval
  553. count += 1
  554. j += 1
  555. return result, lindexer, rindexer
  556. # ----------------------------------------------------------------------
  557. # asof_join_by
  558. # ----------------------------------------------------------------------
  559. from pandas._libs.hashtable cimport (
  560. HashTable,
  561. Int64HashTable,
  562. PyObjectHashTable,
  563. UInt64HashTable,
  564. )
  565. ctypedef fused asof_t:
  566. uint8_t
  567. uint16_t
  568. uint32_t
  569. uint64_t
  570. int8_t
  571. int16_t
  572. int32_t
  573. int64_t
  574. float
  575. float64_t
  576. ctypedef fused by_t:
  577. object
  578. int64_t
  579. uint64_t
  580. def asof_join_backward_on_X_by_Y(asof_t[:] left_values,
  581. asof_t[:] right_values,
  582. by_t[:] left_by_values,
  583. by_t[:] right_by_values,
  584. bint allow_exact_matches=True,
  585. tolerance=None):
  586. cdef:
  587. Py_ssize_t left_pos, right_pos, left_size, right_size, found_right_pos
  588. ndarray[intp_t] left_indexer, right_indexer
  589. bint has_tolerance = False
  590. asof_t tolerance_ = 0
  591. asof_t diff = 0
  592. HashTable hash_table
  593. by_t by_value
  594. # if we are using tolerance, set our objects
  595. if tolerance is not None:
  596. has_tolerance = True
  597. tolerance_ = tolerance
  598. left_size = len(left_values)
  599. right_size = len(right_values)
  600. left_indexer = np.empty(left_size, dtype=np.intp)
  601. right_indexer = np.empty(left_size, dtype=np.intp)
  602. if by_t is object:
  603. hash_table = PyObjectHashTable(right_size)
  604. elif by_t is int64_t:
  605. hash_table = Int64HashTable(right_size)
  606. elif by_t is uint64_t:
  607. hash_table = UInt64HashTable(right_size)
  608. right_pos = 0
  609. for left_pos in range(left_size):
  610. # restart right_pos if it went negative in a previous iteration
  611. if right_pos < 0:
  612. right_pos = 0
  613. # find last position in right whose value is less than left's
  614. if allow_exact_matches:
  615. while (right_pos < right_size and
  616. right_values[right_pos] <= left_values[left_pos]):
  617. hash_table.set_item(right_by_values[right_pos], right_pos)
  618. right_pos += 1
  619. else:
  620. while (right_pos < right_size and
  621. right_values[right_pos] < left_values[left_pos]):
  622. hash_table.set_item(right_by_values[right_pos], right_pos)
  623. right_pos += 1
  624. right_pos -= 1
  625. # save positions as the desired index
  626. by_value = left_by_values[left_pos]
  627. found_right_pos = (hash_table.get_item(by_value)
  628. if by_value in hash_table else -1)
  629. left_indexer[left_pos] = left_pos
  630. right_indexer[left_pos] = found_right_pos
  631. # if needed, verify that tolerance is met
  632. if has_tolerance and found_right_pos != -1:
  633. diff = left_values[left_pos] - right_values[found_right_pos]
  634. if diff > tolerance_:
  635. right_indexer[left_pos] = -1
  636. return left_indexer, right_indexer
  637. def asof_join_forward_on_X_by_Y(asof_t[:] left_values,
  638. asof_t[:] right_values,
  639. by_t[:] left_by_values,
  640. by_t[:] right_by_values,
  641. bint allow_exact_matches=1,
  642. tolerance=None):
  643. cdef:
  644. Py_ssize_t left_pos, right_pos, left_size, right_size, found_right_pos
  645. ndarray[intp_t] left_indexer, right_indexer
  646. bint has_tolerance = False
  647. asof_t tolerance_ = 0
  648. asof_t diff = 0
  649. HashTable hash_table
  650. by_t by_value
  651. # if we are using tolerance, set our objects
  652. if tolerance is not None:
  653. has_tolerance = True
  654. tolerance_ = tolerance
  655. left_size = len(left_values)
  656. right_size = len(right_values)
  657. left_indexer = np.empty(left_size, dtype=np.intp)
  658. right_indexer = np.empty(left_size, dtype=np.intp)
  659. if by_t is object:
  660. hash_table = PyObjectHashTable(right_size)
  661. elif by_t is int64_t:
  662. hash_table = Int64HashTable(right_size)
  663. elif by_t is uint64_t:
  664. hash_table = UInt64HashTable(right_size)
  665. right_pos = right_size - 1
  666. for left_pos in range(left_size - 1, -1, -1):
  667. # restart right_pos if it went over in a previous iteration
  668. if right_pos == right_size:
  669. right_pos = right_size - 1
  670. # find first position in right whose value is greater than left's
  671. if allow_exact_matches:
  672. while (right_pos >= 0 and
  673. right_values[right_pos] >= left_values[left_pos]):
  674. hash_table.set_item(right_by_values[right_pos], right_pos)
  675. right_pos -= 1
  676. else:
  677. while (right_pos >= 0 and
  678. right_values[right_pos] > left_values[left_pos]):
  679. hash_table.set_item(right_by_values[right_pos], right_pos)
  680. right_pos -= 1
  681. right_pos += 1
  682. # save positions as the desired index
  683. by_value = left_by_values[left_pos]
  684. found_right_pos = (hash_table.get_item(by_value)
  685. if by_value in hash_table else -1)
  686. left_indexer[left_pos] = left_pos
  687. right_indexer[left_pos] = found_right_pos
  688. # if needed, verify that tolerance is met
  689. if has_tolerance and found_right_pos != -1:
  690. diff = right_values[found_right_pos] - left_values[left_pos]
  691. if diff > tolerance_:
  692. right_indexer[left_pos] = -1
  693. return left_indexer, right_indexer
  694. def asof_join_nearest_on_X_by_Y(asof_t[:] left_values,
  695. asof_t[:] right_values,
  696. by_t[:] left_by_values,
  697. by_t[:] right_by_values,
  698. bint allow_exact_matches=True,
  699. tolerance=None):
  700. cdef:
  701. Py_ssize_t left_size, right_size, i
  702. ndarray[intp_t] left_indexer, right_indexer, bli, bri, fli, fri
  703. asof_t bdiff, fdiff
  704. left_size = len(left_values)
  705. right_size = len(right_values)
  706. left_indexer = np.empty(left_size, dtype=np.intp)
  707. right_indexer = np.empty(left_size, dtype=np.intp)
  708. # search both forward and backward
  709. bli, bri = asof_join_backward_on_X_by_Y(
  710. left_values,
  711. right_values,
  712. left_by_values,
  713. right_by_values,
  714. allow_exact_matches,
  715. tolerance,
  716. )
  717. fli, fri = asof_join_forward_on_X_by_Y(
  718. left_values,
  719. right_values,
  720. left_by_values,
  721. right_by_values,
  722. allow_exact_matches,
  723. tolerance,
  724. )
  725. for i in range(len(bri)):
  726. # choose timestamp from right with smaller difference
  727. if bri[i] != -1 and fri[i] != -1:
  728. bdiff = left_values[bli[i]] - right_values[bri[i]]
  729. fdiff = right_values[fri[i]] - left_values[fli[i]]
  730. right_indexer[i] = bri[i] if bdiff <= fdiff else fri[i]
  731. else:
  732. right_indexer[i] = bri[i] if bri[i] != -1 else fri[i]
  733. left_indexer[i] = bli[i]
  734. return left_indexer, right_indexer
  735. # ----------------------------------------------------------------------
  736. # asof_join
  737. # ----------------------------------------------------------------------
  738. def asof_join_backward(asof_t[:] left_values,
  739. asof_t[:] right_values,
  740. bint allow_exact_matches=True,
  741. tolerance=None):
  742. cdef:
  743. Py_ssize_t left_pos, right_pos, left_size, right_size
  744. ndarray[intp_t] left_indexer, right_indexer
  745. bint has_tolerance = False
  746. asof_t tolerance_ = 0
  747. asof_t diff = 0
  748. # if we are using tolerance, set our objects
  749. if tolerance is not None:
  750. has_tolerance = True
  751. tolerance_ = tolerance
  752. left_size = len(left_values)
  753. right_size = len(right_values)
  754. left_indexer = np.empty(left_size, dtype=np.intp)
  755. right_indexer = np.empty(left_size, dtype=np.intp)
  756. right_pos = 0
  757. for left_pos in range(left_size):
  758. # restart right_pos if it went negative in a previous iteration
  759. if right_pos < 0:
  760. right_pos = 0
  761. # find last position in right whose value is less than left's
  762. if allow_exact_matches:
  763. while (right_pos < right_size and
  764. right_values[right_pos] <= left_values[left_pos]):
  765. right_pos += 1
  766. else:
  767. while (right_pos < right_size and
  768. right_values[right_pos] < left_values[left_pos]):
  769. right_pos += 1
  770. right_pos -= 1
  771. # save positions as the desired index
  772. left_indexer[left_pos] = left_pos
  773. right_indexer[left_pos] = right_pos
  774. # if needed, verify that tolerance is met
  775. if has_tolerance and right_pos != -1:
  776. diff = left_values[left_pos] - right_values[right_pos]
  777. if diff > tolerance_:
  778. right_indexer[left_pos] = -1
  779. return left_indexer, right_indexer
  780. def asof_join_forward(asof_t[:] left_values,
  781. asof_t[:] right_values,
  782. bint allow_exact_matches=True,
  783. tolerance=None):
  784. cdef:
  785. Py_ssize_t left_pos, right_pos, left_size, right_size
  786. ndarray[intp_t] left_indexer, right_indexer
  787. bint has_tolerance = False
  788. asof_t tolerance_ = 0
  789. asof_t diff = 0
  790. # if we are using tolerance, set our objects
  791. if tolerance is not None:
  792. has_tolerance = True
  793. tolerance_ = tolerance
  794. left_size = len(left_values)
  795. right_size = len(right_values)
  796. left_indexer = np.empty(left_size, dtype=np.intp)
  797. right_indexer = np.empty(left_size, dtype=np.intp)
  798. right_pos = right_size - 1
  799. for left_pos in range(left_size - 1, -1, -1):
  800. # restart right_pos if it went over in a previous iteration
  801. if right_pos == right_size:
  802. right_pos = right_size - 1
  803. # find first position in right whose value is greater than left's
  804. if allow_exact_matches:
  805. while (right_pos >= 0 and
  806. right_values[right_pos] >= left_values[left_pos]):
  807. right_pos -= 1
  808. else:
  809. while (right_pos >= 0 and
  810. right_values[right_pos] > left_values[left_pos]):
  811. right_pos -= 1
  812. right_pos += 1
  813. # save positions as the desired index
  814. left_indexer[left_pos] = left_pos
  815. right_indexer[left_pos] = (right_pos
  816. if right_pos != right_size else -1)
  817. # if needed, verify that tolerance is met
  818. if has_tolerance and right_pos != right_size:
  819. diff = right_values[right_pos] - left_values[left_pos]
  820. if diff > tolerance_:
  821. right_indexer[left_pos] = -1
  822. return left_indexer, right_indexer
  823. def asof_join_nearest(asof_t[:] left_values,
  824. asof_t[:] right_values,
  825. bint allow_exact_matches=True,
  826. tolerance=None):
  827. cdef:
  828. Py_ssize_t left_size, right_size, i
  829. ndarray[intp_t] left_indexer, right_indexer, bli, bri, fli, fri
  830. asof_t bdiff, fdiff
  831. left_size = len(left_values)
  832. right_size = len(right_values)
  833. left_indexer = np.empty(left_size, dtype=np.intp)
  834. right_indexer = np.empty(left_size, dtype=np.intp)
  835. # search both forward and backward
  836. bli, bri = asof_join_backward(left_values, right_values,
  837. allow_exact_matches, tolerance)
  838. fli, fri = asof_join_forward(left_values, right_values,
  839. allow_exact_matches, tolerance)
  840. for i in range(len(bri)):
  841. # choose timestamp from right with smaller difference
  842. if bri[i] != -1 and fri[i] != -1:
  843. bdiff = left_values[bli[i]] - right_values[bri[i]]
  844. fdiff = right_values[fri[i]] - left_values[fli[i]]
  845. right_indexer[i] = bri[i] if bdiff <= fdiff else fri[i]
  846. else:
  847. right_indexer[i] = bri[i] if bri[i] != -1 else fri[i]
  848. left_indexer[i] = bli[i]
  849. return left_indexer, right_indexer