Parse dates into real timestamps
A date stored as text sorts alphabetically and has no calendar meaning. pd.to_datetime turns it into a Timestamp so pandas can order it, compare it, and extract parts such as the month or weekday.
Python
Text becomes datetime64
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Make time the index
Setting the parsed date as the index and sorting it gives you a time-ordered frame. Every time-series operation after this point can rely on that order.
Python
Sorted by time, not by arrival order
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Once the DatetimeIndex exists, daily.loc["2026-02"] selects a calendar month. Without a datetime index, that label lookup will not work.
Resample calendar periods
resample groups observations into fixed time periods. Use MS for the start of a month and ME for the end of a month. The aggregation then decides what happens inside each period.
Python
Monthly totals
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
Rolling windows
A rolling window computes a statistic over a fixed number of observations. The first rows are NaN until the window has enough points. The index must be ordered, otherwise the window mixes the wrong days.
Python
A seven-observation moving average
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
rolling(7) counts observations, not calendar days. With a complete daily series those are the same thing; with gaps they are not, so decide which meaning your question requires.
Derive calendar fields
The .dt accessor exposes parts of each timestamp. These derived fields are useful for grouping by weekday, filtering a month, or labeling a chart.
Python
Calendar parts without string parsing
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The next lesson changes the shape of the table itself. Time-based aggregation groups rows; pivoting moves values between rows and columns.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Parse the date column, set it as the index, sort the rows by time, then resample orders by month start and sum. Assign the Series to monthly.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress