I have created a resampled data frame (DF1) in pandas with a datetimeindex
. I have a separate dataframe (DF2) with a datetimeindex
and time
column. If an instance of time
from DF2 falls within the 30 min bins of datetimeindex
in DF1. I want to mark each instance of time
in DF2 with the appropriate speed
from the 30 min bin in DF1.
DF1
boat_id speed
time
2015-01-13 09:00:00 28.000000 0.000000
2015-01-13 09:30:00 28.000000 0.723503
2015-01-13 10:00:00 28.000000 2.239399
DF2
id boat_id time state
time
2015-01-18 16:09:03 319437 28 2015-01-18 16:09:03 2
2015-01-18 16:18:43 319451 28 2015-01-18 16:18:43 0
2015-03-01 09:39:51 507108 31 2015-03-01 09:39:51 1
2015-03-01 09:40:58 507109 31 2015-03-01 09:40:58 0
Desired Result
id boat_id time state speed
time
2015-01-18 16:09:03 319437 28 2015-01-18 16:09:03 2 nan
2015-01-18 16:18:43 319451 28 2015-01-18 16:18:43 0 nan
2015-03-01 09:39:51 507108 31 2015-03-01 09:39:51 1 2.239399
2015-03-01 09:40:58 507109 31 2015-03-01 09:40:58 0 2.239399
I created this script to try and do this but I think it's failing because datetimeindex
of DF1 is immutable and so my timedelta
request doesn't create a start point for the chunk. One thought I had was if it would be possible to copy the datetimeindex
of DF1 into a new column where the objects are mutable but I haven't managed it yet so am not 100% sure of the logic. I'm happy to tinker but at the moment I've been stalled for a while so was hoping someone else might have a few ideas.
for row in DF1.iterrows():
for dfrow in DF2.iterrows():
if dfrow[0] > row[0] - dt.timedelta(minutes=30) and dfrow[0] < row[0]:
df['test'] = row[1]