3

I've got a few pandas dataframes indexed with timestamps and I would like to merge them into one dataframe, matching nearest timestamp. So I would like to have for example:

a = 
                         CPU
2021-03-25 13:40:44.208  70.571797
2021-03-25 13:40:44.723  14.126870
2021-03-25 13:40:45.228  17.182844

b = 
                          X   Y
2021-03-25 13:40:44.193   45  1
2021-03-25 13:40:44.707   46  1
2021-03-25 13:40:45.216   50  2

a + b =
                         CPU       X   Y
2021-03-25 13:40:44.208  70.571797 45  1
2021-03-25 13:40:44.723  14.126870 46  1
2021-03-25 13:40:45.228  17.182844 50  2

What exact timestamp there is going to be in final DataFrame is not important to me.

BTW. Is there an easy way to leter convert "absolute" timestamps into time from start (either in seconds or miliseconds)? So for this example:


     CPU       X   Y
0.0  70.571797 45  1
0.5  14.126870 46  1
1.0  17.182844 50  2
konserw
  • 494
  • 2
  • 10

1 Answers1

2

Use merge_asof with direction='nearest':

pd.merge_asof(df1, df2, left_index=True, right_index=True, direction='nearest')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252