σμ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
  • 日本語
  • 中文
Parquet skill›Module 2 · Reading less›Topic 2.2·~45 minIntermediate

Hive-style partition layouts

Skill positionModule 2 of 3 · Topic 2 of 2
2.2 Hive-style partition layouts
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Write the folders, then filter on them
  3. 03Choose partition columns with care
  4. 04What the tasks ask

Statistics skip blocks inside a file, but the reader still has to open the file to read them. When a lake holds thousands of files, even opening them costs time. Partitioning moves the decision outside the files: the folder names say what each one holds, so a filter can discard whole files from a directory listing.

01

The idea in plain words

Where we are. Topic 4 skipped row groups with their statistics; this topic skips files with their folder names, the same idea one level up. The DuckDB skill's last topic already wrote a partitioned lake; here we look at how the layout is chosen and what it costs. The next topic is what happens when the columns of those files change over time.

DefinitionHive-style partitioning

Splitting a dataset into folders named column=value, one level per partitioning column. The values live in the folder names rather than in the files, and a reader turns them back into columns, so a filter on them can skip whole folders.

02

Write the folders, then filter on them

write_hive partitions the bars by symbol and then by year, so a file sits at a path like symbol=KO/year=2020/. Asking for the files of one symbol with fragments_for matches the filter against the folder names only, and returns just that symbol's files.

main.pyPython
# Write the bars partitioned by symbol and year, and see the foldersleaves = write_hive(table, "lake", ["symbol", "year"])print(len(leaves), "folders, for example:")print(leaves[0], "|", leaves[-1])print(fragments_for("lake", "KO"), "of", len(leaves), "files are KO's")
135 folders, for example:
symbol=AAPL/year=2000 | symbol=XOM/year=2026
27 of 135 files are KO's
main.pyPython
# Read one symbol-year: the reader opens a single folder and rebuilds the partition columnsko = read_slice("lake", "KO", 2020)print(ko.num_rows, "rows;", ko.column_names)print("first:", ko["date"][0], "| last:", ko["date"][-1])
253 rows; ['symbol', 'date', 'close', 'volume', 'year']
first: 2020-01-02 | last: 2020-12-31
03

Choose partition columns with care

A good partition column is one you filter on, with a modest number of values. symbol and year fit: a handful of symbols times twenty-seven years. date does not: it would make a folder and a tiny file for every trading day, and the cost of listing and opening them would outweigh any saving. The number of distinct values is the number of folders you get.

main.pyPython
# What partitioning by date would cost: one folder per distinct valuefor column in ("symbol", "year", "date"):    print(f"{column:7} -> {len(set(table[column].to_pylist())):>5} distinct values = that many folders per level")
symbol  ->     5 distinct values = that many folders per level
year    ->    27 distinct values = that many folders per level
date    ->  6699 distinct values = that many folders per level
The partition columns are not in the files

Open one file from symbol=KO/year=2020/ on its own and symbol and year are missing: they exist only in the path. Read the dataset through a dataset API and they come back as columns. Copy a file out of its folder and the information goes with the folder, so move partitions as folders, never as loose files.

04

What the tasks ask

  1. 1
    `with_year`table.append_column("year", pc.year(table["date"])).
  2. 2
    `write_hive`pq.write_to_dataset(table, root, partition_cols=...), then walk root and collect the folders that hold files.
  3. 3
    `fragments_for` and `read_slice`Open ds.dataset(root, format="parquet", partitioning="hive"); count get_fragments(filter=...) and read with to_table(filter=...), where the filter is built with ds.field(...).

Key takeaways

  • Folder names are metadata A filter on a partition column discards whole files from a listing.
  • Few values, filtered often That is the profile of a good partition column. date is not one.
  • Move folders, not files The partition values live in the path. Next: when the columns change over time.
Quick check
A lake is partitioned by date, one folder per trading day, with ten rows per file. What is the most likely problem?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Add the year column: with_year25 pts
  • Write the folders: write_hive25 pts
  • Count the files a filter opens: fragments_for25 pts
  • Read one symbol-year: read_slice25 pts
Explorer · lab-5-1
▾ lab-5-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-5-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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