I have a data-frame, X, that contains the following
Index A B
2020-09-08 0.252167 0.263719
2020-09-05 0.266898 0.270347
2019-09-04 0.254873 0.273878
I have another data-frame, Y, that contains the following
Index C
2021-09-08 0.252167
2015-09-05 0.266898
For every row in Y I want to efficiently select the nearest row in X and join them together. Here 'nearest' as function of the index, i.e: which date is closer.
In this case this should return.
Index Index2 C A B
2021-09-08 2020-09-08 0.252167 0.252167 0.263719
2015-09-05 2019-09-04 0.266898 0.254873 0.273878
(note: both indexes are datetime objects)
Since 2020-09-08 is the closest to 2021-09-08 and 2019-09-04 is the closest to 2015-09-05.
I can do this, by iterating through each index of Y and calling
X.index.get_loc(currentYIndex, "nearest")
Is there a more efficient way of doing this ?