Working with Dates, Trends, and Time-Based Business Measurements¶
Notebook Guide¶
Time-indexed data behaves differently from ordinary tabular data. Pandas provides dedicated tools for date-aware analysis.
Learning objectives¶
parse and index dates correctly
filter by time windows
compute rolling summaries
connect time-based summaries to trend interpretation
import pandas as pd
sales_ts = pd.DataFrame(
{
"date": pd.date_range("2026-01-01", periods=6, freq="D"),
"sales": [100, 115, 108, 125, 140, 138],
}
)
sales_ts = sales_ts.set_index("date")
sales_ts["rolling_mean"] = sales_ts["sales"].rolling(window=3).mean()
print(sales_ts)
print("\nJanuary 3 onward")
print(sales_ts.loc["2026-01-03":]) sales rolling_mean
date
2026-01-01 100 NaN
2026-01-02 115 NaN
2026-01-03 108 107.666667
2026-01-04 125 116.000000
2026-01-05 140 124.333333
2026-01-06 138 134.333333
January 3 onward
sales rolling_mean
date
2026-01-03 108 107.666667
2026-01-04 125 116.000000
2026-01-05 140 124.333333
2026-01-06 138 134.333333
Core Explanation¶
Time series analysis depends on ordering, calendar logic, and window-based reasoning. Once dates are parsed correctly, pandas makes it much easier to filter, resample, and summarize sequential business activity.
Exercises¶
Resample the daily series to weekly totals.
Compute a 2-day rolling average instead of a 3-day average.
Add another column for costs and compare sales with profit over time.
8. Interactive Code¶
Expected output
[105.0, 115.0, 125.0]Expected output
309. Guided Practice¶
What makes time-series data different from unordered observations?¶
It has no sequence at allTime-series data is fundamentally ordered.
The order over time mattersCorrect. Sequence and timing are central.
It cannot contain numbersTime series often contains numeric measurements.
It must always be monthlyTime series can be daily, hourly, yearly, and more.
What is the total growth from the first to the last value in the example?¶
20Check the first and last values again.
30Correct. $130 - 100 = 30$.
25That is not the difference shown.
40That is too large for the example.