Module 8 Lesson: Time Series and Rolling Analysis
Let us begin
Time changes the meaning of data.
A total can look high only because the period is longer. A trend can look positive only because one week had missing data. A comparison can be unfair if one course started earlier than another.
This module teaches practical time-based analysis. It is not a forecasting module.
Parse dates
Date columns often arrive as text.
activity["event_date"] = pd.to_datetime(
activity["event_date"],
errors="coerce"
)
After parsing, check failures:
activity["event_date"].isna().sum()
If many dates failed, stop and inspect before summarizing.
Sort by time
activity = activity.sort_values("event_date")
Sorting makes trends easier to inspect and helps rolling calculations behave as expected.
Date parts
Create useful date fields:
activity["year"] = activity["event_date"].dt.year
activity["month"] = activity["event_date"].dt.to_period("M")
activity["weekday"] = activity["event_date"].dt.day_name()
These fields help grouping and plotting.
Resampling
Resampling summarizes data into regular time periods.
daily = (
activity
.set_index("event_date")
.resample("D")
.agg(total_minutes=("minutes_spent", "sum"))
)
Monthly:
monthly = (
activity
.set_index("event_date")
.resample("M")
.agg(total_minutes=("minutes_spent", "sum"))
)
Make sure the time period matches the question.
Rolling windows
A rolling average smooths short-term noise.
daily["rolling_7_day_minutes"] = (
daily["total_minutes"]
.rolling(window=7, min_periods=1)
.mean()
)
Rolling averages are easier to read than daily spikes, but they can hide sudden changes.
Expanding windows
An expanding window starts at the beginning and grows.
daily["cumulative_minutes"] = daily["total_minutes"].cumsum()
Cumulative values are useful for progress, but they always move upward unless negative values exist.
Compare equal windows
Bad comparison:
> Course A has more activity than Course B.
Maybe Course A has been open for six months and Course B for two weeks.
Better comparison:
> During the first four weeks after launch, Course A had more recorded activity than Course B.
Always compare similar windows.
Time zone note
Time zones can change dates and day boundaries. In this course, we keep examples simple. In real projects, ask how timestamps were recorded and whether time zones matter.
Mini worked example
Question:
> How did weekly activity change during the first eight weeks?
Code:
weekly = (
activity
.set_index("event_date")
.resample("W")
.agg(total_minutes=("minutes_spent", "sum"))
)
weekly["rolling_3_week_minutes"] = (
weekly["total_minutes"]
.rolling(window=3, min_periods=1)
.mean()
)
Interpretation:
> The rolling line smooths week-to-week noise. It should not be treated as a forecast.
Takeaway
Time analysis needs careful dates, fair windows, and honest language. In the next module, we bring cleaning, summaries, joins, and time together for exploratory data analysis.
