import pandas
as
pd
# Input dataframes
df_1 = pd.DataFrame({
'timestamp'
: [
'2016-05-14 10:54:33'
,
'2016-05-14 10:54:34'
,
'2016-05-14 10:54:35'
,
'2016-05-14 10:54:36'
,
'2016-05-14 10:54:39'
],
'A'
: [0.020228, 0.057780, 0.098808, 0.158789, 0.038129],
'B'
: [0.026572, 0.175499, 0.620986, 1.014819, 2.384590]
})
df_2 = pd.DataFrame({
'start'
: [
'2016-05-14 10:54:31'
,
'2016-05-14 10:54:34'
,
'2016-05-14 10:54:38'
],
'end'
: [
'2016-05-14 10:54:33'
,
'2016-05-14 10:54:37'
,
'2016-05-14 10:54:42'
],
'event'
: [
'E1'
,
'E2'
,
'E3'
]
})
# Create interval index
df_2.index = pd.IntervalIndex.from_arrays(df_2[
'start'
], df_2[
'end'
], closed=
'both'
)
# Join dataframes using get_loc
df_1[
'event'
] = df_1[
'timestamp'
].apply(lambda x: df_2.iloc[df_2.index.get_loc(x)][
'event'
])
# Output joined dataframe
print
(df_1)