I have a DF created by merging my original DF and a resampled version of the original. The resampled version is Bin_time
and ave_knots
which was merged on the joint field ID
to create this DF.
id trip_id knots times Bin_time ave_knots
0 5045493 10789 6.4924256 5/6/15 17:48 5/6/15 17:30 3.376727771
1 5045494 10789 12.537768 5/6/15 17:53 5/6/15 18:00 2.592866578
2 5045495 10789 9.136048 5/6/15 18:03 5/6/15 18:30 1.109755927
3 5045496 10789 6.4341104 5/6/15 18:04 5/6/15 19:00 1.447413169
4 5045497 10789 10.7688736 5/6/15 18:04 5/6/15 19:30 1.900643556
5 5045498 10789 10.41250293 5/6/15 18:09
6 5045499 10789 11.22891573 5/6/15 18:41
7 5045500 10789 12.04532853 5/6/15 18:46
8 5045501 10789 12.86174133 5/6/15 19:08
What i'm trying to do is where the timestamps in times
fit into the resampled Bin_time
, append the Bin_time
and ave_knots
to the end of the row. This is a paired down data example, ultimately it will have to work on 3 - 5 million rows.
I have tried a few different methods, e.g. df['test'] = np.where(df.times > (df.Bin_time - dt.timedelta(minutes=30)) & (df.times < df.Bin_time ))
and merging on a single value helper column as suggested here. The problem with this solution is it created a DF that was enormous, by merging the resampled DF with each entry of the original DF. This crashed my computer while trying to run the script on the larger DF's. I have got it to work if I read the DF's into CSV's but this solution is slow and inefficient so i'm trying to keep in Pandas because I believe it should offer a faster solution.
Desired Result
id trip_id knots times Bin_time ave_knots
0 5045493 10789 6.4924256 5/6/15 17:48 5/6/15 17:30 3.376727771
1 5045494 10789 12.537768 5/6/15 17:53 5/6/15 17:30 3.376727771
2 5045495 10789 9.136048 5/6/15 18:03 5/6/15 18:00 2.592866578
3 5045496 10789 6.4341104 5/6/15 18:04 5/6/15 18:00 2.592866578
4 5045497 10789 10.7688736 5/6/15 18:04 5/6/15 18:00 2.592866578
5 5045498 10789 10.41250293 5/6/15 18:09 5/6/15 18:00 2.592866578
6 5045499 10789 11.22891573 5/6/15 18:41 5/6/15 18:30 1.109755927
7 5045500 10789 12.04532853 5/6/15 18:46 5/6/15 18:30 1.109755927
8 5045501 10789 12.86174133 5/6/15 19:08 5/6/15 19:00 1.447413169