Pandas: Bin dates into 30 minute intervals and calculate averages Pandas: Bin dates into 30 minute intervals and calculate averages python-3.x python-3.x

Pandas: Bin dates into 30 minute intervals and calculate averages


Converting to datetime and using pandas.Grouper + Offset Aliases:

df['date'] = pd.to_datetime(df.date)df.groupby(pd.Grouper(key='date', freq='30min')).mean().dropna()    speeddate    2018-09-20 01:30:00     47.0400002018-09-20 07:30:00     26.3114292018-09-20 10:30:00     39.9475002018-09-20 11:00:00     32.298000


Since your date column isn't really a date, it's probably more sensible to convert it to a timedelta that way you don't have a date attached to it.

Then, you can use dt.floor to group into 30 minute bins.

import pandas as pddf['date'] = pd.to_timedelta(df.date)df.groupby(df.date.dt.floor('30min')).mean()

Output:

              speeddate               01:30:00  47.04000007:30:00  26.31142910:30:00  39.94750011:00:00  32.298000