Hi I need to align some time series data with nearest timestamps, so I think pandas.merge_asof
could be a good candidate. However, it does not have an option to set how='outer'
like in the standard merge
method.
An example can be:
df1:
Value1
Time
2020-07-17 14:25:03.535906075 108
2020-07-17 14:25:05.457247019 110
2020-07-17 14:25:07.467777014 126
df2:
Value2
Time
2020-07-17 14:25:03.535018921 222
2020-07-17 14:25:04.545104980 150
2020-07-17 14:25:07.476825953 60
Then for example, do this merge_asof
:
pd.merge_asof(df1, df2, left_index=True, right_index=True, direction='nearest', tolerance=pd.Timedelta('0.3s'))
The results will be:
Value1 Value2
Time
2020-07-17 14:25:03.535906075 108 222.0
2020-07-17 14:25:05.457247019 110 NaN
2020-07-17 14:25:07.467777014 126 60.0
But what I want is:
Value1 Value2
Time
2020-07-17 14:25:03.535906075 108 222.0
2020-07-17 14:25:04.545104980 NaN 150.0 <---- this is the difference
2020-07-17 14:25:05.457247019 110 NaN
2020-07-17 14:25:07.467777014 126 60.0
basically like a full outer join.
Any suggestion? Thanks in advance.
EDIT:
So this is the case with 2 dataframes. What if, say for example, there are 10 dataframes (i.e. df1, df2, ..., df10
) need to do this "nearest" merging, what will be a good way to do?