0Pricing
Learn AI with Python · Lesson

Time Series in Pandas

DatetimeIndex, resample(), rolling(), shift(), date range generation and resampling.

Time Series in Pandas is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Time in pandas

pandas has first-class datetime support. Converting strings to timestamps and indexing by time unlocks resampling, rolling windows, and lag features.

Parsing with to_datetime

pd.to_datetime converts strings or numbers into proper Timestamp objects, inferring most common formats automatically.

import pandas as pd
d = pd.to_datetime(["2026-01-01", "2026-01-05", "2026-02-10"])
print(d)
print(d.dtype)   # datetime64[ns]

Generating Dates with date_range

pd.date_range builds an evenly spaced sequence of timestamps. The freq code controls the step: D days, H hours, M month-end, and so on.

rng = pd.date_range("2026-01-01", periods=5, freq="D")
print(rng)
# 2026-01-01 ... 2026-01-05

Building a DatetimeIndex

Setting a datetime column as the index creates a DatetimeIndex, which enables time-aware slicing and the resample/rolling tools.

ts = pd.Series([10, 12, 9, 15, 11],
               index=pd.date_range("2026-01-01", periods=5, freq="D"))
print(ts)

Partial-String Slicing

With a DatetimeIndex you can slice by partial dates: a year or a month string selects all matching rows.

monthly = pd.Series(range(60),
    index=pd.date_range("2026-01-01", periods=60, freq="D"))
print(monthly["2026-02"].head())   # all of February

Downsampling with resample

resample is groupby for time. Downsample to a coarser frequency, then aggregate, for example daily data into weekly sums.

weekly = monthly.resample("W").sum()
print(weekly.head())

Upsampling and Filling

Resampling to a FINER frequency creates gaps. Combine with .ffill() to carry the last value forward.

hourly = ts.resample("12H").ffill()
print(hourly.head())

Rolling Windows

rolling(window) computes a statistic over a sliding window, the foundation of moving averages that smooth noisy series.

roll = monthly.rolling(window=7).mean()
print(roll.head(10))   # first 6 are NaN (not enough history)

Rolling Variants

Any reduction works on a rolling window: mean, sum, std, min, max. A 7-day rolling std measures recent volatility.

vol = monthly.rolling(7).std()
print(vol.tail())

Lagging with shift

shift(n) moves values forward by n periods, creating lag features and enabling period-over-period change.

df = pd.DataFrame({"sales": [100, 110, 105, 130]})
df["prev"] = df["sales"].shift(1)
df["pct_change"] = (df["sales"] - df["prev"]) / df["prev"]
print(df)

Extracting Date Parts

The .dt accessor exposes components like year, month, dayofweek, useful as model features.

s = pd.Series(pd.date_range("2026-01-01", periods=3, freq="D"))
print(s.dt.dayofweek)   # 0=Monday
print(s.dt.month)

Quick Check

Test your time-series knowledge.

Recap

Time-series toolkit:

  • pd.to_datetime to parse, pd.date_range to generate
  • A DatetimeIndex enables partial-string slicing
  • resample(freq).agg() for up/down-sampling
  • rolling(window) for moving statistics
  • shift(n) for lags; .dt for date parts

Frequently asked questions

Is the “Time Series in Pandas” lesson free?

Yes — the full text of “Time Series in Pandas” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Time Series in Pandas”?

DatetimeIndex, resample(), rolling(), shift(), date range generation and resampling. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Time Series in Pandas” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. GroupBy and Aggregation
  2. Pivot Tables and Cross-Tabulation
  3. Advanced Merging and Joining
  4. Time Series in Pandas
← Back to Learn AI with Python