σμ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 1 · Inside the file›Topic 1.3·~40 minIntermediate

Compression codecs

Skill positionModule 1 of 3 · Topic 3 of 3
1.3 Compression codecs
—Lab score— / 100Total XP—Streak—
Lecture0% read

2 min readCode examples: 2

  1. 01The idea in plain words
  2. 02Measure, do not guess
  3. 03What the tasks ask

Storage is cheap and bandwidth is not. A price lake read from object storage over a network spends most of its time moving bytes, so smaller files are faster files, as long as unpacking them costs little. The compression codec is the dial that trades one against the other, and it costs one argument to turn.

01

The idea in plain words

Where we are. Topic 2 showed the layers of a file and the encodings inside a chunk. Compression is the last step applied to every page, after encoding. It does not change what a reader sees, only how many bytes it fetches and how much CPU it spends unpacking them.

DefinitionCodec

The algorithm that compresses a page's bytes and decompresses them again. Parquet records the codec per column chunk, so files compressed differently can be read by the same reader with no configuration.

02

Measure, do not guess

The ranking depends on the data, so write the same table once per codec and compare the files. compare_codecs does exactly that and returns the sizes in the order you asked. Notice that a codec that wins on prices can lose on a column of repeated labels, and that the differences between real codecs are much smaller than the gap between any of them and none at all.

main.pyPython
# One file per codec: the sizes in bytes (codecs this Python lacks are left out)sizes = compare_codecs(table, ".", codecs)for codec, size in sizes.items():    print(f"{codec:7} {size:>9,} bytes  {size / sizes['none']:.0%} of none")
none    1,498,175 bytes  100% of none
snappy  1,246,066 bytes  83% of none
gzip      938,263 bytes  63% of none
zstd      988,171 bytes  66% of none
main.pyPython
# The codec is recorded in each file, so nothing has to be told to the readerfor codec in codecs:    print(codec, "->", codec_of(codec + ".parquet"))print("smallest:", best_codec(table, ".", codecs))
none -> UNCOMPRESSED
snappy -> SNAPPY
gzip -> GZIP
zstd -> ZSTD
smallest: ('gzip', 938263)
A sensible default, and when to move off it

Snappy is the long-time default because it is cheap to unpack. Zstandard usually gives smaller files at a similar speed and lets you raise the level for data written once and read many times, like an archive of history. Choose by measuring on your own columns, and remember that a slower codec costs CPU on every read.

03

What the tasks ask

  1. 1
    `write_codec`pq.write_table(table, path, compression=codec), then return os.path.getsize(path).
  2. 2
    `codec_of`Read .compression of the first column chunk of the first row group.
  3. 3
    `compare_codecs` and `best_codec`Write <codec>.parquet for each codec and collect the sizes in order; then take the smallest (first wins a tie) as (name, bytes).

Key takeaways

  • Compression is the last layer It changes bytes fetched and CPU spent, never what the reader sees.
  • The codec lives in the file No reader setting is needed; mixed codecs in one lake just work.
  • Measure on your data Write once per codec and compare. Next: reading less with statistics.
Quick check
A reader opens a file written with Zstandard. What must it be told about the codec?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Write with a codec: write_codec25 pts
  • Read the codec from the footer: codec_of25 pts
  • Compare codecs on one table: compare_codecs25 pts
  • Pick the smallest: best_codec25 pts
Explorer · lab-3-1
▾ lab-3-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-3-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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