We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Resample

Daily, weekly, and monthly rollups are the bread and butter of product analytics, and resample() is built for exactly that.

It's kinda like groupby(), but for time periods. It needs datetime-like values, either in a DatetimeIndex or in a column passed with on=.

events = events.set_index("timestamp")
print(events.resample("D").size())

Which prints something like:

timestamp
2023-12-29    3
2023-12-30    3
2023-12-31    4
2024-01-01    4
2024-01-02    4
2024-01-03    5
Freq: D, dtype: int64

The set_index() method moves the timestamp column into the index so pandas can group rows by time. Some of the common frequency codes for resampling are:

Code Period
"h" Hourly
"D" Daily
"W" Weekly (ends Sunday)
"ME" Month end

I always start an event analysis with a (usually daily) event count chart. If there are gaps, spikes, or drops, you want to know about them before you start calculating metrics.

Assignment

SnackStack's dashboards need event counts per time bucket.

Complete the get_event_counts function. It accepts an events DataFrame with a timestamp column and a freq frequency code, and returns a DataFrame of event counts, one row per bucket.