σμ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
  • 日本語
  • 中文
DuckDB skill›Module 1 · Querying data in place›Topic 1.2·~40 minIntermediate

Reading Parquet and CSV directly

Skill positionModule 1 of 3 · Topic 2 of 4
1.2 Reading Parquet and CSV directly
—Lab score— / 100Total XP—Streak—
Lecture0% read

2 min readCode examples: 4

  1. 01The idea in plain words
  2. 02A file is a table
  3. 03Parquet: smaller, and read only in part
  4. 04What the tasks ask

A vendor sends five CSV files, one per stock, 33,493 rows in all. Before DuckDB, the first hour went into loading them into something you could query. Here you point SQL at the files and read the answer, and when the same data is stored as Parquet the query touches only the columns and blocks it needs.

01

The idea in plain words

Where we are. Topic 1 queried data that was already in Python. This one queries files directly. The next topic joins two time series that do not share timestamps, with DuckDB's ASOF JOIN.

DefinitionColumn pruning and predicate pushdown

Two savings a columnar file allows. Column pruning reads only the columns the query names. Predicate pushdown uses each block's stored minimum and maximum to skip blocks that cannot hold a matching row, before reading them.

02

A file is a table

main.pyPython
# Put a file name where a table name goes, and ask DuckDB what it foundprint(csv_summary(con, "data/KO.csv"))print(con.sql("DESCRIBE SELECT * FROM read_csv_auto('data/KO.csv')"))1
(6699, datetime.date(2000, 1, 3), datetime.date(2026, 8, 21))
┌─────────────┬─────────────┬─────────┬─────────┬─────────┬─────────┐
│ column_name │ column_type │  null   │   key   │ default │  extra  │
│   varchar   │   varchar   │ varchar │ varchar │ varchar │ varchar │
├─────────────┼─────────────┼─────────┼─────────┼─────────┼─────────┤
│ date        │ DATE        │ YES     │ NULL    │ NULL    │ NULL    │
│ open        │ DOUBLE      │ YES     │ NULL    │ NULL    │ NULL    │
│ high        │ DOUBLE      │ YES     │ NULL    │ NULL    │ NULL    │
│ low         │ DOUBLE      │ YES     │ NULL    │ NULL    │ NULL    │
│ close       │ DOUBLE      │ YES     │ NULL    │ NULL    │ NULL    │
│ volume      │ BIGINT      │ YES     │ NULL    │ NULL    │ NULL    │
└─────────────┴─────────────┴─────────┴─────────┴─────────┴─────────┘
  1. 1DuckDB sniffed the delimiter, the header and each column's type from the file.

A glob reads many files as one table. Adding filename = true gives every row a column with the file it came from, so one query can count the rows of every file at once.

main.pyPython
# Five files, one queryprint(rows_per_file(con, "data/*.csv"))
{'JNJ.csv': 6699, 'JPM.csv': 6698, 'KO.csv': 6699, 'AAPL.csv': 6699, 'XOM.csv': 6698}
03

Parquet: smaller, and read only in part

main.pyPython
# Write a CSV as zstd Parquet and compare sizesimport osprint(csv_to_parquet(con, "data/JPM.csv", "jpm.parquet"), "rows written")print("CSV bytes:", os.path.getsize("data/JPM.csv"), "| Parquet bytes:", os.path.getsize("jpm.parquet"))print(con.sql("SELECT row_group_id, row_group_num_rows, compression FROM parquet_metadata('jpm.parquet') WHERE path_in_schema = 'close'"))1
6698 rows written
CSV bytes: 340314 | Parquet bytes: 144313
┌──────────────┬────────────────────┬─────────────┐
│ row_group_id │ row_group_num_rows │ compression │
│    int64     │       int64        │   varchar   │
├──────────────┼────────────────────┼─────────────┤
│            0 │               6698 │ ZSTD        │
└──────────────┴────────────────────┴─────────────┘
  1. 1parquet_metadata reads the file's footer: how many row groups, and how each column is compressed.

EXPLAIN shows what a query will read. For a Parquet file with four columns, a query for two columns and one symbol lists just those columns as projections and the symbol and date tests as filters, handed to the Parquet reader instead of applied afterwards.

main.pyPython
# Read two columns of one symbol; the plan shows what was pushed into the file readercon.execute("COPY (SELECT symbol, date, close, volume FROM bars) TO 'bars.parquet' (FORMAT parquet)").fetchall()print(parquet_slice(con, "bars.parquet", ["date", "close"], "JPM", "2026-08-19"))print(con.sql("EXPLAIN SELECT date, close FROM read_parquet('bars.parquet') WHERE symbol = 'JPM' AND date >= '2026-08-19'").fetchall()[0][1])
[(datetime.date(2026, 8, 19), 357.26), (datetime.date(2026, 8, 20), 351.55), (datetime.date(2026, 8, 21), 351.58)]
╭─ Read Parquet ───────────────────────╮
│ Function: READ_PARQUET               │
│ Projections: date, close             │
│ Filters: symbol = 'JPM',             │
│          date >= '2026-08-19'::DATE  │
│ ~6,698 rows                          │
╰──────────────────────────────────────╯
A path is data too

A file name goes into the SQL text as a string literal, so a name with a single quote (it's data/prices.csv) breaks a hand-built statement. Pass every path through quote() and every column name through ident(), as the earlier SQL topics did for values.

04

What the tasks ask

  1. 1
    `csv_summary` and `rows_per_file`Query a CSV and a glob of CSVs with read_csv_auto and read_csv; DuckDB reads the files, not Python.
  2. 2
    `csv_to_parquet`COPY (query) TO file (FORMAT parquet, COMPRESSION zstd), returning the row count.
  3. 3
    `parquet_slice`Select only the requested columns, filter on symbol and date, and reject an unknown column with a clear error.

Key takeaways

  • Files are tables read_csv_auto, read_parquet and globs need no import step.
  • Parquet reads less Name only the columns you need and filter early, so the reader prunes columns and skips blocks.
  • Quote every path and name quote() for paths, ident() for column names. Next: ASOF JOIN.
Quick check
A Parquet file has columns symbol, date, close and volume. Which query reads the least of it?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • A CSV as a table: csv_summary25 pts
  • A glob of files: rows_per_file25 pts
  • CSV to Parquet: csv_to_parquet25 pts
  • Read only what you need: parquet_slice25 pts
Explorer · lab-2-1
▾ lab-2-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-2-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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