σμ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
  • 日本語
  • 中文
Apache Arrow skill›Module 2 · Memory layout, sharing and files›Topic 2.4·~45 minAdvanced

Dictionary encoding and timestamps

Skill positionModule 2 of 3 · Topic 4 of 4
2.4 Dictionary encoding and timestamps
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 2

  1. 01The idea in plain words
  2. 02Store repeated text once
  3. 03A timestamp is an integer with a unit
  4. 04What the tasks ask

Two columns in every market table need special handling, and both are about being explicit. A ticker symbol repeats thousands of times, so storing its text on every row is waste. A timestamp looks like one thing but is an integer plus a unit plus possibly a time zone, and mixing those up shifts trades by hours or by a factor of a thousand. Arrow has a type for each, and knowing them prevents some of the most expensive silent bugs in data work.

01

The idea in plain words

Where we are. Topic 4 showed that a string column pays offsets and characters on every row. Here is the fix, the same idea as Parquet's dictionary encoding: keep each distinct value once. The timestamp half of the topic prepares the capstone, where the ticks carry real times. The last two topics move data between processes.

DefinitionDictionary array

An array made of two parts: a dictionary holding each distinct value once, and a list of small integer indices, one per row, pointing into it. Reading a row means looking its index up in the dictionary.

02

Store repeated text once

dictionary_encode swaps a string column for a dictionary one in place. The dictionary holds the five symbols in order of first appearance and every row becomes a 4-byte index. The values read back identically, so code that only looks at values does not notice, while the column takes about 40% fewer bytes here and the saving grows with longer strings and fewer distinct values.

main.pyPython
# Dictionary-encode the symbol column and compare its sizecoded = dictionary_encode(table, "symbol")print(coded.schema.field("symbol").type)print("bytes:", f"{table['symbol'].nbytes:,}", "->", f"{coded['symbol'].nbytes:,}")values, indices = dictionary_parts(coded["symbol"].chunk(0))print("dictionary:", values)print("first indices:", indices[:5], "| values read back the same:", coded["symbol"].to_pylist() == table["symbol"].to_pylist())
dictionary<values=string, indices=int32, ordered=0>
bytes: 234,451 -> 134,147
dictionary: ['AAPL', 'JPM', 'XOM', 'JNJ', 'KO']
first indices: [0, 0, 0, 0, 0] | values read back the same: True
03

A timestamp is an integer with a unit

The bars carry a date32: days since 1970. An intraday system needs real times, so as_utc_timestamps converts each date to midnight UTC in the unit you choose. timestamp_ints shows what is really stored: the number of units since 1970-01-01. The same instant is 946,857,600 in seconds and 946,857,600,000 in milliseconds, which is why reading a column with the wrong unit gives dates in 1970 or in the year 50,000.

main.pyPython
# Midnight UTC for the first bar, in four units: the same instant, four different integersfor unit in ("s", "ms", "us", "ns"):    stamped = as_utc_timestamps(table, "date", unit)    print(f"{str(stamped.schema.field('date').type):28} {timestamp_ints(stamped['date'].chunk(0))[0]:>22,}")
timestamp[s, tz=UTC]                    946,857,600
timestamp[ms, tz=UTC]               946,857,600,000
timestamp[us, tz=UTC]           946,857,600,000,000
timestamp[ns, tz=UTC]        946,857,600,000,000,000
A time zone is part of the type, not of the number

A timestamp[ms, tz=UTC] and a timestamp[ms] store the same integer; the zone only changes how it is displayed and how local dates are computed. Store trade times in UTC and convert at the edge. Date-only values such as daily bars stay date32, not midnight timestamps, unless a system needs the time.

04

What the tasks ask

  1. 1
    `dictionary_encode` and `dictionary_parts`table.set_column(index, name, pc.dictionary_encode(column)); for a dictionary array, array.dictionary and array.indices.
  2. 2
    `as_utc_timestamps` and `timestamp_ints`column.cast(pa.timestamp(unit, tz="UTC")) and set_column; array.cast(pa.int64()) reveals the integers.

Key takeaways

  • Repeated text: a dictionary Each distinct value once, a small index per row, identical values on read.
  • A timestamp is count, unit and zone The unit and the zone live in the type; read with the wrong unit and every date is wrong.
  • Store UTC, convert at the edge It keeps trades comparable across venues. Next: moving batches between processes.
Quick check
A column of nanosecond timestamps is mistakenly read as milliseconds. What does that do to the dates?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Store repeated text once: dictionary_encode25 pts
  • Take it apart: dictionary_parts25 pts
  • Dates to UTC timestamps: as_utc_timestamps25 pts
  • The integers behind a time: timestamp_ints25 pts
Explorer · lab-7-1
▾ lab-7-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-7-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

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