How to group DataFrame by a period of time?

Since the original answer is rather old and pandas introduced periods a different solution is nowadays:

df.groupby(df.index.to_period('T'))

Additionally, you can resample

df.resample('T')

You can group on any array/Series of the same length as your DataFrame --- even a computed factor that's not actually a column of the DataFrame. So to group by minute you can do:

df.groupby(df.index.map(lambda t: t.minute))

If you want to group by minute and something else, just mix the above with the column you want to use:

df.groupby([df.index.map(lambda t: t.minute), 'Source'])

Personally I find it useful to just add columns to the DataFrame to store some of these computed things (e.g., a "Minute" column) if I want to group by them often, since it makes the grouping code less verbose.

Or you could try something like this:

df.groupby([df['Source'],pd.TimeGrouper(freq='Min')])

pd.TimeGrouper is now depreciated. Here is v1.05 update using pd.Grouper

df['Date'] = df.index

df.groupby(['Source',pd.Grouper(key = 'Date', freq='30min')])

Tags:

Python

Pandas