pandas merging based on a timestamp which do not match exactly
<p>What methods are available to merge columns which have timestamps that do not exactly match?</p>

<p>DF1:</p>

<pre><code>date    start_time  employee_id session_id
01/01/2016  01/01/2016 06:03:13 7261824 871631182
</code></pre>

<p>DF2:</p>

<pre><code>date    start_time  employee_id session_id
01/01/2016  01/01/2016 06:03:37 7261824 871631182
</code></pre>

<p>I could join on the ['date', 'employee_id', 'session_id'], but sometimes the same employee will have multiple identical sessions on the same date which causes duplicates. I could drop the rows where this takes place, but I would lose valid sessions if I did.</p>

<p>Is there an efficient way to join if the timestamp of DF1 is &lt;5 minutes from the timestamp of DF2, and the session_id and employee_id also match? If there is a matching record, then the timestamp will always be slightly later than DF1 because an event is triggered at some future point.</p>

<pre><code>['employee_id', 'session_id', 'timestamp&lt;5minutes']
</code></pre>

<p><strong>Edit</strong> - I assumed someone would have run into this issue before.</p>

<p>I was thinking of doing this:</p>

<ol>
<li>Take my timestamp on each dataframe</li>
<li>Create a column which is the timestamp + 5 minutes (rounded)</li>
<li>Create a column which is the timestamp - 5 minutes (rounded)</li>
<li><p>Create a 10 minute interval string to join the files on</p>

<blockquote>
<pre><code>df1['low_time'] = df1['start_time'] - timedelta(minutes=5)
df1['high_time'] = df1['start_time'] + timedelta(minutes=5)
df1['interval_string'] = df1['low_time'].astype(str) + df1['high_time'].astype(str)
</code></pre>
</blockquote></li>
</ol>

<p>Does someone know how to round those 5 minute intervals to the nearest 5 minute mark?</p>

<p>02:59:37 - 5 min = 02:55:00</p>

<p>02:59:37 + 5 min = 03:05:00</p>

<p>interval_string = '02:55:00-03:05:00'</p>

<pre><code>pd.merge(df1, df2, how = 'left', on = ['employee_id', 'session_id', 'date', 'interval_string']
</code></pre>

<p>Does anyone know how to round the time like that? This seems like it could work. You still match based on the date, employee, and session, and then you look for times which are basically within the same 10 minute interval or range</p>