Best way to combine datetime in pandas dataframe when times are close
<p>What is the best way to combine times together if they are very close (within 5 seconds).</p>

<pre><code>                   start                 end   delta
0    2016-01-01 08:00:01 2016-01-01 08:07:53   472.0
1    2016-01-01 08:07:54 2016-01-01 08:09:23    89.0
2    2016-01-01 08:09:24 2016-01-01 08:32:51  1407.0
3    2016-01-01 08:38:56 2016-01-01 08:38:58     2.0
4    2016-01-01 08:39:00 2016-01-01 08:58:06  1146.0
5    2016-01-01 09:07:26 2016-01-01 09:07:27     1.0
6    2016-01-01 09:07:31 2016-01-01 09:07:33     2.0
7    2016-01-01 09:07:35 2016-01-01 09:11:28   233.0
</code></pre>

<p>becomes</p>

<pre><code>                   start                 end   delta
0    2016-01-01 08:00:01 2016-01-01 08:07:53   472.0
1    2016-01-01 08:07:54 2016-01-01 08:32:51  1496.0
2    2016-01-01 08:38:56 2016-01-01 08:58:06  1148.0
3    2016-01-01 09:07:26 2016-01-01 09:11:28   236.0
</code></pre>

<p>Try this:</p>

<pre><code>timediff = df.start.diff()/np.timedelta64(1, 's')
pd.DataFrame(
    {'start': df[(timediff&gt;5) | (timediff.isnull())].start.tolist(),
    'end': df[(timediff.shift(-1)&gt;5) |  (timediff.shift(-1).isnull())].end.tolist()}
    )
</code></pre>

<p>This will give you start and end  columns. Then you can take the delta.</p>

python pandas numpy