σμ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.2·~45 minAdvanced

Zero-copy between libraries

Skill positionModule 2 of 3 · Topic 2 of 4
2.2 Zero-copy between libraries
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01The idea in plain words
  2. 02Prove it with addresses
  3. 03pandas: share, or convert and copy
  4. 04What the tasks ask

A research pipeline passes the same prices through four libraries: PyArrow reads the file, Polars cleans it, pandas feeds a model, DuckDB answers a query. If each hand-off converts the data, the pipeline spends most of its time and memory making copies. With Arrow as the shared layout, a hand-off can be a pointer instead: the next library looks at the very same bytes.

01

The idea in plain words

Where we are. Topic 4 showed that an array is a few buffers with a fixed layout. This topic is the payoff: a library that understands the layout can use the buffers as they are. The DuckDB skill showed the query side of this; here the focus is on proving that no copy happened, by comparing the buffer addresses.

DefinitionZero-copy

Passing data between libraries or functions so that the receiver reads the sender's memory instead of a duplicate. It costs the same for ten rows or ten billion, and it makes the shared data read-only, because a change would be seen by everyone.

02

Prove it with addresses

buffers()[1].address is where the values of the close array start in memory. numpy_view returns a NumPy array whose own data pointer is that same number: same bytes, no copy, and NumPy marks the array read-only because it does not own it. Polars does the same for its numeric columns. When a conversion would have to copy, as for a column with nulls, Arrow refuses instead of copying quietly.

main.pyPython
# A NumPy array over the closes: same address as the Arrow buffer, read-onlyimport pyarrow.compute as pcview = numpy_view(table, "close")print(type(view).__name__, view.dtype, "| writeable:", view.flags.writeable)print("same bytes as the Arrow buffer:", view.__array_interface__["data"][0] == close.buffers()[1].address)
ndarray float64 | writeable: False
same bytes as the Arrow buffer: True
main.pyPython
# Polars on the same buffers; the sum computed from either side agreesframe = polars_view(table)print(type(frame).__name__, frame.shape)print("close buffer shared with Polars:", frame["close"].to_arrow().buffers()[1].address == close.buffers()[1].address)print(frame["volume"].sum(), pc.sum(table["volume"]).as_py())
DataFrame (33493, 7)
close buffer shared with Polars: True
3398140608402 3398140608402
03

pandas: share, or convert and copy

pandas historically stores columns as NumPy arrays and strings as Python objects, so a plain to_pandas() copies every column. With types_mapper=pd.ArrowDtype the DataFrame keeps its columns as Arrow arrays and shares the buffers. The price is that some pandas features expect NumPy, so try it where memory is the problem. DuckDB, last, needs no conversion at all: register the Arrow table and it scans it where it is.

main.pyPython
# A plain to_pandas() copies into NumPy; with Arrow dtypes the buffer is sharedplain = table.to_pandas()arrow_backed = pandas_arrow(table)print("default to_pandas shares memory :", plain["close"].values.__array_interface__["data"][0] == close.buffers()[1].address)print("Arrow-backed shares memory       :", arrow_backed["close"].array.__arrow_array__().chunk(0).buffers()[1].address == close.buffers()[1].address)print(arrow_backed.dtypes.tolist()[:3])
default to_pandas shares memory : False
Arrow-backed shares memory       : True
[string[pyarrow], date32[day][pyarrow], double[pyarrow]]
main.pyPython
# DuckDB scans the Arrow table where it is, with no load stepprint("total volume:", duckdb_total(table, "volume"))print("matches Arrow's own sum:", duckdb_total(table, "volume") == pc.sum(table["volume"]).as_py())
total volume: 3398140608402
matches Arrow's own sum: True
Shared memory is read-only

Everyone sees the same bytes, so a library that writes into them would change the data for all. NumPy views from Arrow are read-only, and a copy() is the explicit, visible way to get something you can modify. The pipeline principle: share while reading, copy deliberately when you must change.

04

What the tasks ask

  1. 1
    `numpy_view` and `polars_view`table[column].chunk(0).to_numpy(zero_copy_only=True); pl.from_arrow(table).
  2. 2
    `pandas_arrow`table.to_pandas(types_mapper=pd.ArrowDtype).
  3. 3
    `duckdb_total`con.register("t", table), then SELECT sum("column") FROM t with the name quoted; read the single value.

Key takeaways

  • Same layout, same bytes Libraries that understand Arrow read each other's buffers directly.
  • Check with addresses Equal buffer addresses prove that nothing was copied.
  • Read-only by design Copy on purpose when you must write. Next: saving Arrow to disk unchanged.
Quick check
to_numpy(zero_copy_only=True) raises an error on a column with nulls. What is the right way to read that?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • A NumPy view: numpy_view25 pts
  • Hand it to Polars: polars_view25 pts
  • pandas on Arrow: pandas_arrow25 pts
  • DuckDB reads it in place: duckdb_total25 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