fork(2) download
  1. import pandas
  2.  
  3. df1 = pandas.DataFrame([[1.002,18],[2,22],[3,77],[5,23]], columns=["timestamp", "max_val"])
  4. df2 = pandas.DataFrame([[1,33],[2,12],[3.001,87],[4,54]], columns=["timestamp", "max_val"])
  5.  
  6. df2['Rank'] = df2['timestamp'].rank()
  7.  
  8. merged_df = (pandas.merge_asof(df1, df2,
  9. on='timestamp',
  10. direction='nearest',
  11. tolerance=0.01)
  12. .merge(df2, on='Rank', how='outer')
  13. .assign(timestamp = lambda x: x.filter(like='timestamp').bfill(1).iloc[:,0])
  14. .assign(max_val=lambda x: x.filter(like='max_val').max(1))
  15. .sort_values('timestamp')
  16. [['timestamp','max_val']]
  17. )
  18.  
  19. print(df1)
  20. print(df2)
  21. print(merged_df)
Success #stdin #stdout 0.27s 62344KB
stdin
Standard input is empty
stdout
   timestamp  max_val
0      1.002       18
1      2.000       22
2      3.000       77
3      5.000       23
   timestamp  max_val  Rank
0      1.000       33   1.0
1      2.000       12   2.0
2      3.001       87   3.0
3      4.000       54   4.0
   timestamp  max_val
0      1.002     33.0
1      2.000     22.0
2      3.000     87.0
4      4.000     54.0
3      5.000     23.0