Panda group dataframe based on datetime type into different period ignoring date part
<p>I want to group the rows into groups, based on variable time interval.
However, when doing grouping, I want to ignore the date part, only group based on the time date. </p>

<p>Say I want to group every 5 minutes.</p>

<pre><code>       timestampe            val
0  2016-08-11 11:03:00        0.1
1  2016-08-13 11:06:00        0.3
2  2016-08-09 11:04:00        0.5
3  2016-08-05 11:35:00        0.7
4  2016-08-19 11:09:00        0.8
5  2016-08-21 12:37:00        0.9

        into 

       timestampe             val
0  2016-08-11 11:03:00        0.1
2  2016-08-09 11:04:00        0.5

       timestampe             val
1  2016-08-13 11:06:00        0.3
4  2016-08-19 11:09:00        0.8

       timestampe             val
3  2016-08-05 11:35:00        0.7
       timestampe             val
5  2016-08-21 12:37:00        0.9
</code></pre>

<p>Notice as long as the time is within the same 5 minutes interval, the rows are grouped, regardless of the date.</p>

<p>Since you do not care about the <code>date</code> part of your <code>datetime</code> object, I think that make all <code>date</code> equal is a good trick.</p>

<pre><code>df['time'] = df['timestamp'].apply(lambda x: x.replace(year=2000, month=1, day=1))
</code></pre>

<p>You get:</p>

<pre><code>            timestamp  val                time
0 2016-08-11 11:03:00  0.1 2000-01-01 11:03:00
1 2016-08-13 11:06:00  0.3 2000-01-01 11:06:00
2 2016-08-09 11:04:00  0.5 2000-01-01 11:04:00
3 2016-08-05 11:35:00  0.7 2000-01-01 11:35:00
4 2016-08-19 11:09:00  0.8 2000-01-01 11:09:00
5 2016-08-21 11:37:00  0.9 2000-01-01 11:37:00
</code></pre>

<p>Now you can do what you what on <code>time</code> column. For example, groups on every 5 mins:</p>

<pre><code>grouped = df.groupby(Grouper(key='time', freq='5min'))

grouped.count()

                     timestamp  val
time                               
2000-01-01 11:00:00          2    2
2000-01-01 11:05:00          2    2
2000-01-01 11:10:00          0    0
2000-01-01 11:15:00          0    0
2000-01-01 11:20:00          0    0
2000-01-01 11:25:00          0    0
2000-01-01 11:30:00          0    0
2000-01-01 11:35:00          2    2
</code></pre>

<p>Hope this trick may be suitable for your need. Thanks!</p>

python datetime pandas numpy scipy