Pandas how to split dataframe by column by interval
<p>I have a gigantic dataframe with a datetime type column called <code>dt</code>, the data frame is sorted based on <code>dt</code> already. I want to split the dataframe into several dataframes based on <code>dt</code>, each dataframe contains rows within <code>1 hr</code> range.</p>

<p>Split</p>

<pre><code>   dt                    text
0  20160811 11:05        a
1  20160811 11:35        b
2  20160811 12:03        c
3  20160811 12:36        d
4  20160811 12:52        e
5  20160811 14:32        f
</code></pre>

<p>into </p>

<pre><code>   dt                    text
0  20160811 11:05        a
1  20160811 11:35        b
2  20160811 12:03        c

   dt                    text
0  20160811 12:36        d
1  20160811 12:52        e

   dt                    text 
0  20160811 14:32        f
</code></pre>

<p>take the difference of dates with first date and group by total_seconds</p>

<pre><code>df.groupby((df.dt - df.dt[0]).dt.total_seconds() // 3600,
           as_index=False).apply(pd.DataFrame.reset_index, drop=True)
</code></pre>

<p><a href="http://i.stack.imgur.com/9MxQ6.png" rel="nofollow"><img src="http://i.stack.imgur.com/9MxQ6.png" alt="enter image description here"></a></p>

python python-2.7 pandas numpy scipy