ref_url: http://deanla.com/dont_reinvent_pandas.html#Resample ref_name: DeanLa's Blog ---

Resample

Task: How many events happen each hour?

The Old Way

bad = df.copy()
bad['day'] = bad.index.date
bad['hour'] = bad.index.hour
(bad
.groupby(['day','hour'])
.count()
)
event_type
day hour
2018-10-09 17 83
18 96
20 91
21 71
22 84

A Better Way

df.resample('H').count() # H is for Hour
event_type
date_
2018-10-09 17:00:00 83
2018-10-09 18:00:00 96
2018-10-09 19:00:00 0
2018-10-09 20:00:00 91
2018-10-09 21:00:00 71
2018-10-09 22:00:00 84