σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Python skill›Module 2 · NumPy & pandas for market data›Topic 2.12·~40 minIntermediate

DatetimeIndex and time-based indexing

Skill positionModule 2 of 11 · Topic 12 of 20
2.12 DatetimeIndex and time-based indexing
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01Dates are not strings
  2. 02Building a calendar
  3. 03Selecting by date
  4. 04Parts of a date
01

Dates are not strings

A price series is a time series: what matters is when each price happened. Dates read from a file are plain strings, and a string has no idea that March comes after February or that a Monday follows a Sunday. Convert them with pd.to_datetime and use the result as the index: a DatetimeIndex holds real timestamps that pandas can sort, slice, compare and take apart. Always sort it (sort_index()) so that the oldest observation comes first.

Python
1
2
3
4
5
6
7
8
9
10
11
12
13import pandas as pd

raw_dates = ["2026-03-04", "2026-03-02", "2026-03-03"]      # strings, in a messy order
closes = [102.0, 100.0, 101.0]

series = pd.Series(closes, index=pd.to_datetime(raw_dates), name="close").sort_index()
print(series)
# 2026-03-02    100.0
# 2026-03-03    101.0
# 2026-03-04    102.0
# Name: close, dtype: float64

print(isinstance(series.index, pd.DatetimeIndex))   # True
02

Building a calendar

You often need dates that nobody gave you: a grid to compare against, or the days of a backtest. pd.date_range(start, periods=n, freq="D") makes n consecutive calendar days, and pd.bdate_range skips weekends and gives business days. (It knows nothing about holidays.) The freq argument sets the step: "D" is a day, "B" a business day, "h" an hour.

Python
1
2
3
4
5
6
7# Four calendar days, then six business days (the weekend is skipped)
print(pd.date_range("2026-03-01", periods=4, freq="D").strftime("%a %d %b").tolist())
# ['Sun 01 Mar', 'Mon 02 Mar', 'Tue 03 Mar', 'Wed 04 Mar']

days = pd.bdate_range("2026-02-26", periods=6)
print(days.strftime("%a %d %b").tolist())
# ['Thu 26 Feb', 'Fri 27 Feb', 'Mon 02 Mar', 'Tue 03 Mar', 'Wed 04 Mar', 'Thu 05 Mar']
03

Selecting by date

With a sorted DatetimeIndex, .loc understands dates written as strings. A partial string such as "2026-03" selects the whole month, "2026" the whole year and "2026-03-04" a single day. A slice .loc["2026-03-04":"2026-03-06"] selects a range, and unlike a slice of positions both ends are included. The ends need not exist in the data: weekend dates simply match nothing, which is what you want when you ask for "everything from the first of the month".

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15prices = pd.Series(
    [float(i) for i in range(20)],
    index=pd.bdate_range("2026-02-23", periods=20),   # Mon 23 Feb ... Fri 20 Mar
)

print(len(prices.loc["2026-03"]))      # 15   the business days of March in the data
print(prices.loc["2026-03-04"])        # 7.0  a single day

print(prices.loc["2026-03-04":"2026-03-06"])
# 2026-03-04    7.0
# 2026-03-05    8.0
# 2026-03-06    9.0
# Freq: B, dtype: float64

print(prices.loc["2026-03-01":"2026-03-03"].tolist())   # [5.0, 6.0]   Sunday the 1st matches nothing
04

Parts of a date

A DatetimeIndex exposes the parts of every date as attributes: year, month, day, dayofweek (Monday is 0, Sunday is 6). Each one is an array with one entry per row, so it can be used as a mask or as a group key. This is how you ask "how many observations fall on each weekday?" or "what happens on Mondays?" without a loop. Remember to sort the index first: slicing a DatetimeIndex that is out of order can give surprising results or an error.

Python
1
2
3
4
5
6
7
8
9# The parts of the dates, one entry per row
print(prices.index.dayofweek[:7].tolist())   # [0, 1, 2, 3, 4, 0, 1]   Monday is 0
print(prices.index.month[:7].tolist())       # [2, 2, 2, 2, 2, 3, 3]

# How many observations fall on each weekday?
print(prices.groupby(prices.index.dayofweek).size().to_dict())   # {0: 4, 1: 4, 2: 4, 3: 4, 4: 4}

# What do the Mondays look like?
print(prices[prices.index.dayofweek == 0].tolist())   # [0.0, 5.0, 10.0, 15.0]
Mind the end of the range

A date string without a time means midnight. With daily data .loc["2026-03-04":"2026-03-06"] includes the 6th, but with intraday data it stops at 00:00 on the 6th, losing the whole day. Check the last timestamp of the result when your data is finer than a day.

  • pd.to_datetime(strings) makes a DatetimeIndex; sort_index() puts the oldest first; date_range and bdate_range build calendars.
  • .loc["2026-03"] selects a month, .loc["2026-03-04":"2026-03-06"] a range with both ends included.
  • index.year, index.month, index.dayofweek give the parts of every date, to filter or group without loops.

Next up: resampling, turning daily prices into weekly, monthly or even finer bars.

Lab · what's graded

4 tasks · 100 pts
  • Build a dated price series with parse_prices25 pts
  • Select one month with prices_in_month25 pts
  • Select a date range with prices_between25 pts
  • Count the observations per weekday with count_by_weekday25 pts
Explorer · lab-45-1
▾ lab-45-1
…
bash · in-browser
learner@quantcraft:~/lab-45-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-45-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes