Group DataFrame by period of time with aggregation
<p>I am using Pandas to structure and process Data. This is my DataFrame:</p>

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

<p>I grouped many datetimes by minute and I did an aggregation in order to have the sum of 'bitrate' scores by minute.
This was my code to have this Dataframe:</p>

<pre><code>def aggregate_data(data):

    def delete_seconds(time):

        return (datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S')).replace(second=0)


    data['new_time'] = data['beginning_time'].apply(delete_seconds)
    df = (data[['new_time', 'bitrate']].groupby(['new_time'])).aggregate(np.sum)

    return df
</code></pre>

<p>Now I want to do a similar thing with 5 minutes as buckets. I wand to do group my datetimes by 5 minutes and do a mean.. 
Something like this : (This dosent work of course!)</p>

<pre><code>df.groupby([df.index.map(lambda t: t.5minute)]).aggregate(np.mean)
</code></pre>

<p>Ideas ? Thx !</p>

<p>use <a href="http://pandas.pydata.org/pandas-docs/stable/timeseries.html#resampling" rel="nofollow">resample</a>. </p>

<p><code>
df.resample('5Min').sum()
</code></p>

<p>This assumes your index is properly set as a DateTimeIndex. </p>

<p>you can also use the TimeGrouper, as resampling is really just a groupby operation on time buckets. </p>

<p><code>
df.groupby(pd.TimeGrouper('5Min')).sum()
</code></p>

python pandas group-by aggregate aggregation