pandas.merge_asof

pandas 3.1.1 · General functions · function

Match each ordered key to a nearby key in the other table. Sort the keys first and use direction or tolerance to control the match.

Call syntax

pd.merge_asof(…) Full signature
pd.merge_asof(
    left,
    right,
    on=None,
    left_on=None,
    right_on=None,
    left_index=False,
    right_index=False,
    by=None,
    left_by=None,
    right_by=None,
    suffixes=('_x', '_y'),
    tolerance=None,
    allow_exact_matches=True,
    direction='backward',
)

Run the example

Edit the example and select Run Python to see the result.

Parameters

ParameterRequirementAccepted values and behavior
left Required
Positional or keyword
DataFrame or named Series

Supply the left-hand input or the left endpoint of the interval.

right Required
Positional or keyword
DataFrame or named Series

Supply the right-hand input or the right endpoint of the interval.

on Optional
Positional or keyword
Default None
label

Choose the shared key columns or name the axis column used by the operation.

left_on Optional
Positional or keyword
Default None
label or array-like

Identify the join keys in the left-hand object.

right_on Optional
Positional or keyword
Default None
label or array-like

Identify the join keys in the right-hand object.

left_index Optional
Positional or keyword
Default False
bool

Use the left object's index labels as join keys.

right_index Optional
Positional or keyword
Default False
bool

Use the right object's index labels as join keys.

by Optional
Positional or keyword
Default None
column name or list of column names

Specify the labels, mapping, or values that determine the sort or grouping.

left_by Optional
Positional or keyword
Default None
column name

Group or partition the left input by these keys before the match.

right_by Optional
Positional or keyword
Default None
column name

Group or partition the right input by these keys before the match.

suffixes Optional
Positional or keyword
Default ('_x', '_y')
2-length sequence (tuple, list, ...)

Supply a pair for resolving duplicate column names in joined tables.

tolerance Optional
Positional or keyword
Default None
int or timedelta

Set the maximum acceptable distance for an inexact match.

allow_exact_matches Optional
Positional or keyword
Default True
bool
direction Optional
Positional or keyword
Default 'backward'
'backward' (default), 'forward', or 'nearest'

Returns

DataFrame