Pandas: how to merge two dataframes on offset dates?

前端 未结 2 772
萌比男神i
萌比男神i 2021-01-22 09:13

I\'d like to merge two dataframes, df1 & df2, based on whether rows of df2 fall within a 3-6 month date range after rows of df1. For example:

df1 (for each company I

2条回答
  •  醉梦人生
    2021-01-22 09:19

    This is actually one of those rare questions where the algorithmic complexity might be significantly different for different solutions. You might want to consider this over the niftiness of 1-liner snippets.

    Algorithmically:

    • sort the larger of the dataframes according to the date

    • for each date in the smaller dataframe, use the bisect module to find the relevant rows in the larger dataframe

    For dataframes with lengths m and n, respectively (m < n) the complexity should be O(m log(n)).

提交回复
热议问题