I want to merge the dataframe df2
to the closest time of df1
.df2
has less rows than df1
.Those are time-series with different sampling times (in seconds):
df1
A B
1 s 1 2
2 s 2 4
3 s 3 5
df2
C D
0.2 s 1 2
1.7 s 2 4
df3 = pd.merge_asof(df1,df2,left_index=True, right_index=True)
df3
A B C D
1 s 1 2 1 2
2 s 2 4 2 4
3 s 3 5 2 4
Pandas is filling in the missing rows with a zero-hold interpolation:
but I would like to have NaN instead in the row that is missing like the following:
df3
A B C D
1 s 1 2 1 2
2 s 2 4 2 4
3 s 3 5 Nan NaN
What's the most elegant way to achieve this?