Skip to content
Advertisement

How to prevent data from being recycled when using pd.merge_asof in Python

I am looking to join two data frames using the pd.merge_asof function. This function allows me to match data on a unique id and/or a nearest key. In this example, I am matching on the id as well as the nearest date that is less than or equal to the date in df1.

Is there a way to prevent the data from df2 being recycled when joining?

This is the code that I currently have that recycles the values in df2.

JavaScript

This is the output that I would like to see instead where only the first match is successful

Targeted Outcome

Advertisement

Answer

Given your merge direction is backward, you can do a mask on duplicated id and df2’s date after merge_asof:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement