一个简单的解决方案是
interval index从
start and end设置中创建
closed = both然后用于
get_loc获取事件,即(希望所有日期时间都在timestamps dtype中)
df_2.index = pd.IntervalIndex.from_arrays(df_2['start'],df_2['end'],closed='both')df_1['event'] = df_1['timestamp'].apply(lambda x : df_2.iloc[df_2.index.get_loc(x)]['event'])
输出:
时间戳AB事件0 2016-05-14 10:54:33 0.020228 0.026572 E11 2016-05-14 10:54:34 0.057780 0.175499 E22 2016-05-14 10:54:35 0.098808 0.620986 E23 2016-05-14 10:54:36 0.158789 1.014819 E24 2016-05-14 10:54:39 0.038129 2.384590 E3



