σμ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 1 · Arrays, tables and kernels›Topic 1.2·~40 minIntermediate

Tables and record batches

Skill positionModule 1 of 3 · Topic 2 of 3
1.2 Tables and record batches
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Cut a table into batches and put it back
  3. 03Work one batch at a time
  4. 04Make a table fit a schema
  5. 05What the tasks ask

Ticks do not arrive as one finished table. They arrive in pieces, a few thousand at a time, and the pipeline that handles them cannot wait for the day to end. Arrow's answer is the record batch: a small, complete slab of rows that can be computed on, sent or saved the moment it exists, then replaced by the next one.

01

The idea in plain words

Where we are. Topic 1 showed what a column is. This topic groups columns into batches and tables, which are the objects every Arrow tool passes around. The next topic computes on them, and the later IPC and Flight topics are about sending exactly these batches.

DefinitionRecord batch

A set of equal-length arrays with a schema: one contiguous slab of rows. It is the unit Arrow computes on, streams and writes to files. A table is a list of chunks, usually one per batch.

02

Cut a table into batches and put it back

The bars table is not one block. It was built from five stocks, so it has five chunks, and to_batches never makes a batch that straddles two of them. Asking for batches of at most 10,000 rows therefore gives five batches of about 6,700, not four of 10,000. table_from_batches goes the other way and wraps the same memory in a table, one chunk per batch, without copying a value.

main.pyPython
# Cut the bars into batches of at most 10,000 rows, then wrap the batches in a table againsizes = batch_sizes(table, 10000)print(len(sizes), "batches:", sizes)batches = table.to_batches(max_chunksize=10000)again = table_from_batches(batches)print("rows:", again.num_rows, "| chunks per column:", again["close"].num_chunks, "| equal:", again.equals(table))
5 batches: [6699, 6698, 6698, 6699, 6699]
rows: 33493 | chunks per column: 5 | equal: True
03

Work one batch at a time

batches_total adds up a column across batches without ever joining them: it computes a partial sum per batch and carries the running total. Only one batch needs to be in memory at a time, so the same code works on a day of data or ten years, as long as the batches arrive from a stream. A null-only batch adds nothing, which is why pc.sum returns None for it and the code treats that as zero.

main.pyPython
# Add up the volume batch by batch and compare with summing the whole table at onceimport pyarrow.compute as pcprint("by batches :", batches_total(batches, "volume"))print("whole table:", pc.sum(table["volume"]).as_py())
by batches : 3398140608402
whole table: 3398140608402
04

Make a table fit a schema

Two systems agree on a schema or they do not talk. conform takes the columns a consumer expects, in its order, with its types, and drops the rest. cast converts safely: it raises an error rather than silently overflowing, which is why a price can become float32 but a share volume of three billion cannot become int32.

main.pyPython
# A consumer wants volume, close and symbol, in that order, with narrower types for close and symboltarget = pa.schema([("volume", pa.int64()), ("close", pa.float32()), ("symbol", pa.large_string())])fitted = conform(table, target)print(fitted.schema)try:    conform(table, pa.schema([("volume", pa.int32())]))except Exception as error:    print(type(error).__name__ + ": volume does not fit in int32")
volume: int64
close: float
symbol: large_string
ArrowInvalid: volume does not fit in int32
Batch size is a trade-off

Too small and every batch pays fixed costs for its header and function calls; too large and a batch no longer fits in cache or arrives late. Thousands to a few hundred thousand rows is the usual range. Pick it from the slowest stage of the pipeline and measure, rather than copying a number.

05

What the tasks ask

  1. 1
    `batch_sizes` and `table_from_batches`table.to_batches(max_chunksize=...) and take each num_rows; pa.Table.from_batches(batches).
  2. 2
    `batches_total`For each batch, add pc.sum(batch[column]).as_py() or 0; no from_batches, no to_pylist.
  3. 3
    `conform`table.select(schema.names).cast(schema).

Key takeaways

  • Batches are the unit Compute, send and save batch by batch; a table is a list of them.
  • Wrapping does not copy Batches into a table, and back, share the same memory.
  • Casts are checked A value that does not fit raises an error. Next: computing on columns.
Quick check
table.to_batches(max_chunksize=10000) on a table with five chunks of about 6,700 rows gives how many batches?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Cut into batches: batch_sizes25 pts
  • Put them back together: table_from_batches25 pts
  • Sum without one big table: batches_total25 pts
  • Match a target schema: conform25 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