| _increasing: | |
| if isna(values).any(): | |
| raise ValueError(f"Merge keys contain null values on {side} side") | |
| raise ValueError(f"{side} keys must be sorted") | |
| if isinstance(values, ArrowExtensionArray): | |
| values = values._maybe_convert_datelike_array() | |
| if needs_i8_conversion(values.dtype): | |
| values = values.view("i8") | |
| elif isinstance(values, BaseMaskedArray): | |
| # we've verified above that no nulls exist | |
| values = values._data | |
| elif isinstance(values, ExtensionArray): | |
| values = values.to_numpy() | |
| # error: Incompatible return value type (got "Union[ExtensionArray, | |
| # Any, ndarray[Any, Any], ndarray[Any, dtype[Any]], Index, Series]", | |
| # expected "ndarray[Any, Any]") | |
| return values # type: ignore[return-value] | |
| def _get_join_indexers(self) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: | |
| """return the join indexers""" | |
| # values to compare | |
| left_values = ( | |
| self.left.index._values if self.left_index else self.left_join_keys[-1] | |
| ) | |
| right_values = ( | |
| self.right.index._values if self.right_index else self.right_join_keys[-1] | |
| ) | |
| # _maybe_require_matching_dtypes already checked for dtype matching | |
| assert left_values.dtype == right_values.dtype | |
| tolerance = self.tolerance | |
| if tolerance is not None: | |
| # TODO: can we reuse a tolerance-conversion function from | |
| # e.g. TimedeltaIndex? | |
| if needs_i8_conversion(left_values.dtype): | |
| tolerance = Timedelta(tolerance) | |
| # TODO: we have no test cases with PeriodDtype here; probably | |
| # need to adjust tolerance for that case. | |
| if left_values.dtype.kind in "mM": | |
| # Make sure the i8 representation for tolerance | |
| # matches that for left_values/right_values. | |
| lvs = ensure_wrapped_if_datetimelike(left_values) | |
| tolerance = tolerance.as_unit(lvs.unit) | |
| tolerance = tolerance._value | |
| # initial type conversion as needed | |
| left_values = self._convert_values_for_libjoin(left_values, "left") | |
| right_values = self._convert_values_for_libjoin(right_values, "right") | |
| # a "by" parameter requires special handling | |
| if self.left_by is not None: | |
| # remove 'on' parameter from values if one existed | |
| if self.left_index and self.right_index: | |
| left_join_keys = self.left_join_keys | |
| right_join_keys = self.right_join_keys | |
| else: | |
| left_join_keys = self.left_join_keys[0:-1] | |
| right_join_keys = self.right_join_keys[0:-1] | |
| mapped = [ | |
| _factorize_keys( | |
| left_join_keys[n], | |
| right_join_keys[n], | |
| sort=False, | |
| how="left", | |
| ) | |
| for n in range(len(left_join_keys)) | |
| ] | |
| if len(left_join_keys) == 1: | |
| left_by_values = mapped[0][0] | |
| right_by_values = mapped[0][1] | |
| else: | |
| arrs = [np.concatenate(m[:2]) for m in mapped] | |
| shape = tuple(m[2] for m in mapped) | |
| group_index = get_group_index( | |
| arrs, shape=shape, sort=False, xnull=False | |
| ) | |
| left_len = len(left_join_keys[0]) | |
| left_by_values = group_index[:left_len] | |
| right_by_values = group_index[left_len:] | |
| left_by_values = ensure_int64(left_by_values) | |
| right_by_values = ensure_int64(right_by_values) | |
| # choose appropriate function by type | |
| func = _asof_by_function(self.direction) | |
| return func( | |
| left_values, | |
| right_values, | |
| left_by_values, | |
| right_by_values, | |
| self.allow_exact_matches, | |
| tolerance, | |
| ) | |
| else: | |
| # choose appropriate function by type | |
| func = _asof_by_function(self.direction) | |
| return func( | |
| left_values, | |
| right_values, | |
| None, | |
| None, | |
| self.allow_exact_matches, | |
| tolerance, | |
| False, | |
| ) | |
| def _get_multiindex_indexer( | |
| join_keys: list[ArrayLike], index: MultiIndex, sort: bool | |
| ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: | |
| # left & right join labels and num. of levels at each location | |
| mapped = ( | |
| _factorize_keys(index.levels[n]._values, join_keys[n], sort=sort) | |
| for n in range(index.nlevels) | |
| ) | |
| zipped = zip(*mapped) | |
| rcodes, lcodes, shape = (list(x) for x in zipped) | |
| if sort: | |
| rcodes = list(map(np.take, rcodes, index.codes)) | |
| else: | |
| i8copy = lambda a: a.astype("i8", subok=False, copy=True) | |
| rcodes = list(map(i8copy, index.codes)) | |
| # fix right labels if there were any nulls | |
| for i, join_key in enumerate(join_keys): | |
| mask = index.codes[i] == -1 | |
| if mask.any(): | |
| # check if there already was any nulls at this location | |
| # if there was, it is factorized to `shape[i] - 1` | |
| a = join_key[lcodes[i] == shape[i] - 1] | |
| if a.size == 0 or not a[0] != a[0]: | |
| shape[i] += 1 | |
| rcodes[i][mask] = shape[i] - 1 | |
| # get flat i8 join keys | |
| lkey, rkey = _get_join_keys(lcodes, rcodes, tuple(shape), sort) | |
| return lkey, rkey | |
| def _get_empty_indexer() -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: | |
| """Return empty join indexers.""" | |
| return ( | |
| np.array([], dtype=np.intp), | |
| np.array([], dtype=np.intp), | |
| ) | |
| def _get_no_sort_one_missing_indexer( | |
| n: int, left_missing: bool | |
| ) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: | |
| """ | |
| Return join indexers where all of one side is selected without sorting | |
| and none of the other side is selected. | |
| Parameters | |
| ---------- | |
| n : int | |
| Length of indexers to create. | |
| left_missing : bool | |
| If True, the left indexer will contain only -1's. | |
| If False, the right indexer will contain only -1's. | |
| Returns | |
| ------- | |
| np.ndarray[np.intp] | |
| Left indexer | |
| np.ndarray[np.intp] | |
| Right indexer | |
| """ | |
| idx = np.arange(n, dtype=np.intp) | |
| idx_missing = np.full(shape=n, fill_value=-1, dtype=np.intp) | |
| if left_missing: | |
| return idx_missing, idx | |
| return idx, idx_missing | |
| def _left_join_on_index( | |
| left_ax: Index, right_ax: Index, join_keys: list[ArrayLike], sort: bool = False | |
| ) -> tuple[Index, npt.NDArray[np.intp] | None, npt.NDArray[np.intp]]: | |
| if isinstance(right_ax, MultiIndex): | |
| lkey, rkey = _get_multiindex_indexer(join_keys, right_ax, sort=sort) | |
| else: | |
| # error: Incompatible types in assignment (expression has type | |
| # "Union[Union[ExtensionArray, ndarray[Any, Any]], Index, Series]", | |
| # variable has type "ndarray[Any, dtype[signedinteger[Any]]]") | |
| lkey = join_keys[0] # type: ignore[assignment] | |
| # error: Incompatible types in assignment (expression has type "Index", | |
| # variable has type "ndarray[Any, dtype[signedinteger[Any]]]") | |
| rkey = right_ax._values # type: ignore[assignment] | |
| left_key, right_key, count = _factorize_keys(lkey, rkey, sort=sort) | |
| left_indexer, right_indexer = libjoin.left_outer_join( | |