How to group DataFrame by a period of time?

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')])

Leave a Comment