σμ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
  • 日本語
  • 中文
Polars skill›Module 1 · DataFrames and expressions›Topic 1.1·~40 minBeginner

Your first DataFrame

Skill positionModule 1 of 3 · Topic 1 of 3
1.1 Your first DataFrame
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Read a file and look at it
  3. 03Look at rows and summarise a column
  4. 04What the tasks ask

Almost every quant task starts the same way: a file of prices, and a need to look at it before doing anything else. How many rows? Which columns, and of what type? Are the dates dates or just text? Polars answers these in a line each, and the answers come back fast even for a file with millions of rows, because the table is stored column by column in Arrow memory.

01

The idea in plain words

Where we are. The first topic of the Polars skill and a beginner one. It needs only Python. The next topics transform the columns with expressions, summarise by group, join tables and handle time. If you know pandas, you will find the same ideas with a different style: columns are typed strictly, there is no row index, and work is described as expressions rather than done cell by cell.

DefinitionDataFrame

A table of named columns, each holding values of one type, with all columns the same length. In Polars the columns are Arrow arrays, there is no row index, and a column on its own is called a Series.

02

Read a file and look at it

pl.read_csv(path, try_parse_dates=True) reads the file and turns date-looking text into real Date values, which is what lets later topics group by month or join by time. Without the flag the column stays text and everything date-related fails quietly. describe_frame collects the shape and the dtype of each column, the first things to check on any new file.

main.pyPython
# The shape and the type of every column of the bars tablerows, columns, fields = describe_frame(df)print(rows, "rows,", columns, "columns")for name, dtype in fields:    print(f"{name:8} {dtype}")
33493 rows, 7 columns
symbol   String
date     Date
open     Float64
high     Float64
low      Float64
close    Float64
volume   Int64
main.pyPython
# Write KO's rows to a CSV and read them back with the dates parseddf.filter(pl.col("symbol") == "KO").write_csv("ko_copy.csv")ko = read_bars_file("ko_copy.csv")print(ko.shape, "| date column type:", ko.schema["date"])print(pl.read_csv("ko_copy.csv").schema["date"], "<- without try_parse_dates")
(6699, 7) | date column type: Date
String <- without try_parse_dates
03

Look at rows and summarise a column

head_rows shows the first rows as tuples, one value per column, which is the quickest way to see whether the data looks right. column_summary takes one column, a Series, and asks it for its minimum, maximum, mean and number of nulls. These are methods on the column itself, computed in compiled code over the whole column at once. Min, max and mean skip nulls, so the null count is how you find out how many values were missing.

main.pyPython
# The first two rows, and a summary of the closing pricesfor row in head_rows(df, 2):    print(row)summary = column_summary(df, "close")print({key: round(value, 3) if isinstance(value, float) else value for key, value in summary.items()})
('AAPL', datetime.date(2000, 1, 3), 0.785568, 0.842382, 0.761624, 0.83826, 638640787)
('AAPL', datetime.date(2000, 1, 4), 0.810884, 0.828742, 0.758094, 0.76771, 610726329)
{'min': 0.197, 'max': 365.18, 'mean': 61.862, 'nulls': 0}
Check the types before you trust the numbers

A price column that arrives as String because one cell holds a dash, or a volume that is Float64 because of a stray decimal, will not behave like the numbers you expect, and the errors show up much later. Look at the schema of every new file first. In Polars a column never mixes types, so a wrong dtype is a signal about the data, not something to ignore.

04

What the tasks ask

  1. 1
    `read_bars_file`pl.read_csv(path, try_parse_dates=True).
  2. 2
    `describe_frame` and `head_rows`df.shape, df.schema.items() with str(dtype); df.head(n).rows().
  3. 3
    `column_summary`df[column] is a Series: .min(), .max(), .mean(), .null_count().

Key takeaways

  • Typed columns, no row index Each column has one dtype; a row is just a position.
  • Parse dates on the way in try_parse_dates=True turns text into Date, which later work needs.
  • Look first Shape, schema, first rows, then a summary. Next: expressions.
Quick check
After pl.read_csv("bars.csv") with no other options, df.schema["date"] shows String. What is the fix?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Read a CSV with dates: read_bars_file25 pts
  • Shape and types: describe_frame25 pts
  • The first rows: head_rows25 pts
  • Summarise a column: column_summary25 pts
Explorer · lab-1-1
▾ lab-1-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-1-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-1-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