I have 2 dataframes, left_df
and right_df
, each of which has a column corresponding to datetime. I want to join them in such a way, that for every row R
in left_df
, I find the row in right_df
that is closest in time to R
out of all rows in right_df
, and put them together. I don't have about whether the row from left_df
or from right_df
came first.
An example is given below:
left_df =
left_dt left_flag
0 2014-08-23 07:57:03.827516 True
1 2014-08-23 09:27:12.831126 False
2 2014-08-23 11:55:27.551029 True
3 2014-08-23 16:11:33.511049 True
right_df =
right dt right_flag
0 2014-08-23 07:12:52.80587 True
1 2014-08-23 15:12:34.815087 True
desired output_df =
left_dt left_flag right dt right_flag
0 2014-08-23 07:57:03.827516 True 2015-08-23 07:12:52.80587 True
1 2014-08-23 09:27:12.831126 False 2015-08-23 07:12:52.80587 True
2 2014-08-23 11:55:27.551029 True 2015-08-23 15:12:34.815087 True
3 2014-08-23 16:11:33.511049 True 2015-08-23 15:12:34.815087 True