Skip to content
LearnPY
pandas

Work with dates and time series

Parse date strings, use a DatetimeIndex, resample calendar periods, and compute rolling windows without losing the time order.

12 min readbeginnerpandas

By the end

  • Convert date strings with pd.to_datetime.
  • Sort and index a table by time.
  • Aggregate with resample over calendar periods.
  • Compute rolling statistics on ordered observations.
  • Use the datetime accessor to derive calendar fields.

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01Why set the parsed date as the index?
02Which resample alias represents the start of each month?
03What does rolling(7) count?

Lesson progress