Skip to content

PERF: Remove unnecessary asof join functions #46943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 9 additions & 70 deletions pandas/_libs/join.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,16 @@ def asof_join_nearest_on_X_by_Y(numeric_t[:] left_values,
by_t[:] left_by_values,
by_t[:] right_by_values,
bint allow_exact_matches=True,
tolerance=None):
tolerance=None,
bint use_hashtable=True):

cdef:
ndarray[intp_t] bli, bri, fli, fri

ndarray[intp_t] left_indexer, right_indexer
Py_ssize_t left_size, i
numeric_t bdiff, fdiff

# search both forward and backward
bli, bri = asof_join_backward_on_X_by_Y(
left_values,
Expand All @@ -852,6 +857,7 @@ def asof_join_nearest_on_X_by_Y(numeric_t[:] left_values,
right_by_values,
allow_exact_matches,
tolerance,
use_hashtable
)
fli, fri = asof_join_forward_on_X_by_Y(
left_values,
Expand All @@ -860,26 +866,11 @@ def asof_join_nearest_on_X_by_Y(numeric_t[:] left_values,
right_by_values,
allow_exact_matches,
tolerance,
use_hashtable
)

return _choose_smaller_timestamp(left_values, right_values, bli, bri, fli, fri)


cdef _choose_smaller_timestamp(
numeric_t[:] left_values,
numeric_t[:] right_values,
ndarray[intp_t] bli,
ndarray[intp_t] bri,
ndarray[intp_t] fli,
ndarray[intp_t] fri,
):
cdef:
ndarray[intp_t] left_indexer, right_indexer
Py_ssize_t left_size, i
numeric_t bdiff, fdiff

# choose the smaller timestamp
left_size = len(left_values)

left_indexer = np.empty(left_size, dtype=np.intp)
right_indexer = np.empty(left_size, dtype=np.intp)

Expand All @@ -894,55 +885,3 @@ cdef _choose_smaller_timestamp(
left_indexer[i] = bli[i]

return left_indexer, right_indexer


# ----------------------------------------------------------------------
# asof_join
# ----------------------------------------------------------------------

def asof_join_backward(numeric_t[:] left_values,
numeric_t[:] right_values,
bint allow_exact_matches=True,
tolerance=None):

return asof_join_backward_on_X_by_Y(
left_values,
right_values,
None,
None,
allow_exact_matches=allow_exact_matches,
tolerance=tolerance,
use_hashtable=False,
)


def asof_join_forward(numeric_t[:] left_values,
numeric_t[:] right_values,
bint allow_exact_matches=True,
tolerance=None):
return asof_join_forward_on_X_by_Y(
left_values,
right_values,
None,
None,
allow_exact_matches=allow_exact_matches,
tolerance=tolerance,
use_hashtable=False,
)


def asof_join_nearest(numeric_t[:] left_values,
numeric_t[:] right_values,
bint allow_exact_matches=True,
tolerance=None):

cdef:
ndarray[intp_t] bli, bri, fli, fri

# 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)

return _choose_smaller_timestamp(left_values, right_values, bli, bri, fli, fri)
17 changes: 10 additions & 7 deletions pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,11 +1691,6 @@ def get_result(self) -> DataFrame:
return result


def _asof_function(direction: str):
name = f"asof_join_{direction}"
return getattr(libjoin, name, None)


def _asof_by_function(direction: str):
name = f"asof_join_{direction}_on_X_by_Y"
return getattr(libjoin, name, None)
Expand Down Expand Up @@ -2017,8 +2012,16 @@ def injection(obj):
)
else:
# choose appropriate function by type
func = _asof_function(self.direction)
return func(left_values, right_values, self.allow_exact_matches, tolerance)
func = _asof_by_function(self.direction)
return func(
left_values,
right_values,
None,
None,
self.allow_exact_matches,
tolerance,
False,
)


def _get_multiindex_indexer(
Expand Down