σμ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 3 · Keeping a lake healthy›Topic 3.1·~40 minIntermediate

Schema evolution

Skill positionModule 3 of 3 · Topic 1 of 4
3.1 Schema evolution
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Add a column, read old and new together
  3. 03Widening a type
  4. 04What the tasks ask

A research lake is never finished. Vendors add fields, a volume column becomes 64-bit because a share count overflowed, a new rating appears halfway through the history. The files written last year do not have what the files written today have, yet one query has to read them all. Parquet's answer is that every file carries its own schema.

01

The idea in plain words

Where we are. The previous topics were about reading less from files that all look the same. This one is about files that do not: the layout of the lake changes over time and the reader must reconcile the versions. It uses the partitioned lake of topic 5, whose folders accumulate files written under different schemas.

DefinitionSchema evolution

Changing the columns or types of a dataset over time while old data stays readable. Adding a column and widening a number type are safe; renaming or dropping columns, or changing what a column means, is where datasets break.

02

Add a column, read old and new together

Here the old file has three columns and the new one has a fourth, source. unified_schema merges the footers into one schema without reading any data. read_evolved hands that schema to the dataset reader, which reads each file as it is and fills the missing column with nulls for the old rows. Nothing had to be rewritten.

main.pyPython
# An old file (3 columns) and a newer one with a fourth column, read togetherpq.write_table(table.slice(0, 1000), "old.parquet")pq.write_table(add_column(table.slice(1000, 5), "source", "vendor-b"), "new.parquet")print(unified_schema(["old.parquet", "new.parquet"]))both = read_evolved(["old.parquet", "new.parquet"])print(both.num_rows, "rows;", both["source"].null_count, "nulls in source")print(both.slice(998, 4).to_pylist()[1:3])
[('symbol', 'string'), ('date', 'date32[day]'), ('close', 'double'), ('source', 'string')]
1005 rows; 1000 nulls in source
[{'symbol': 'AAPL', 'date': datetime.date(2003, 12, 24), 'close': 0.305937, 'source': None}, {'symbol': 'AAPL', 'date': datetime.date(2003, 12, 26), 'close': 0.311343, 'source': 'vendor-b'}]
03

Widening a type

A 32-bit volume is fine until one day it is not. Casting to 64 bits keeps every value and is the safe change, but the files already written still say int32, and a reader will not guess. widen rewrites a file with the column cast and leaves the original untouched, so you can validate the new file before swapping it in. Going the other way, narrowing, can silently lose values.

main.pyPython
# A file with float32 closes, rewritten with float64 closes; the original stays as it wassmall = table.slice(0, 1000)small = small.set_column(2, "close", small["close"].cast("float32"))pq.write_table(small, "small.parquet")print("before:", pq.read_schema("small.parquet").field("close").type)print("after :", widen("small.parquet", "big.parquet", "close", "float64"))print("original still:", pq.read_schema("small.parquet").field("close").type)
before: float
after : double
original still: float
main.pyPython
# The danger: two files that disagree on a type cannot be mergedtry:    unified_schema(["old.parquet", "small.parquet"])    print("merged: a float32 and a float64 column do not agree, so this line should not print")except Exception as error:    print(type(error).__name__ + ":", str(error)[:70])
ArrowTypeError: Unable to merge: Field close has incompatible types: double vs float
Renames look like a drop plus an add

Parquet matches columns by name, so renaming px to close in new files makes old files look like they lack close and new files like they lack px. Prefer adding the new name, filling both for a while, and dropping the old one once every reader has moved. Treat a change of meaning (adjusted versus raw prices) as a new column, never as a quiet edit.

04

What the tasks ask

  1. 1
    `add_column`table.append_column(name, pa.array([value] * table.num_rows)).
  2. 2
    `unified_schema` and `read_evolved`pa.unify_schemas([pq.read_schema(p) ...]) gives the merged schema; pass it as schema= to ds.dataset(paths, format="parquet") and call .to_table().
  3. 3
    `widen`Read the table, set_column with the column cast to pa.type_for_alias(new_type), write path_out, return str(type) read back from it.

Key takeaways

  • Files carry their schema Old and new files coexist; the reader merges their footers.
  • Add and widen are safe Renames, drops and changes of meaning are not; add a new column instead.
  • Rewrite beside, then swap Keep the original until the new file is checked. Next: statistics and bloom filters.
Quick check
A vendor renames px to close in files written from 2024. What does a reader that merges all the files by name see?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Add a column: add_column25 pts
  • Merge the schemas: unified_schema25 pts
  • Read files of different shapes: read_evolved25 pts
  • Widen a type safely: widen25 pts
Explorer · lab-6-1
▾ lab-6-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-6-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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