Sub topic 1
Why data tools need a shared in-memory format
Prerequisites
This chapter assumes you can:
- Run Python and install packages with pip.
- Read tables in terms of rows and columns, as in pandas or SQL.
- Tell in-memory data (lives in RAM while a program runs) from on-disk data (stored in files).
Install the library with pip install pyarrow pandas.
The interchange problem
A typical data job uses several tools. A database holds the raw data. A Python script cleans it with pandas. A query engine aggregates it. A machine learning library consumes the result.
Each tool keeps its own internal representation of a table. When data moves between two tools, someone has to convert it. A conversion usually does two expensive things:
- Serialize: walk every value and write it into an intermediate form such as CSV, JSON or pickle.
- Deserialize: parse that form and rebuild the table in the receiving tool's layout.
Both steps cost CPU time, and both create a full second copy of the data in memory. For a table with millions of rows, this can take longer than the actual analysis.
The N-by-M conversion cost
The cost grows with the number of tools. If every tool has its own layout, each pair of tools needs its own converter. With N producers and M consumers, that is up to N × M converters. Each one must be written, tested and kept in sync as the tools change.
A shared in-memory format changes the count. Every tool learns to read and write one layout, so you need roughly N + M adapters instead.
What Apache Arrow provides
Apache Arrow is a specification for how a table is laid out in memory, plus libraries that implement it in many languages (C++, Python, Rust, Java and others). The key properties are:
- Columnar: each column's values sit together in one contiguous run of memory.
- Language independent: a buffer written by C++ code can be read by Rust code with the same meaning.
- Self-describing: a schema states each column's name and type, so a receiver knows how to interpret the bytes.
Because the layout is fixed by the specification, a tool that already holds Arrow data can pass it on without rewriting it. The receiver reads the same bytes. This is called zero-copy sharing, and later chapters cover when it applies.
A first look
This example builds an Arrow table from a pandas DataFrame and prints its schema, the description every Arrow-aware tool can rely on.
import pandas as pd
import pyarrow as pa
df = pd.DataFrame({"id": [1, 2, 3], "score": [0.5, 1.5, 2.5]})
table = pa.Table.from_pandas(df)
print(table.schema)
print(table.num_rows)The output lists id: int64 and score: double, along with some pandas metadata, and reports 3 rows.
Common misunderstandings
- "Arrow replaces Parquet." It does not. Arrow describes data in memory. Parquet is a compressed file format for storage. They are designed to work together.
- "Using Arrow means no copies ever." Zero-copy only holds when both sides agree on the layout. Some conversions, such as pandas columns that contain Python objects, still copy.
- "Every tool already speaks Arrow." Support is wide but not universal. Where a tool does not support it, you pay the old conversion cost.
Exercise
Five tools each need to exchange tables with every one of the other four, and each pair needs a converter in both directions. Roughly how many converters are needed without a shared format, and how many adapters with one?
Show answer
Without a shared format, each of the 5 tools needs a converter to each of the other 4, so 5 × 4 = 20 directional converters. With a shared format, each tool needs one adapter that reads it and one that writes it, so about 5 to 10 adapters, depending on whether you count reading and writing separately. The count grows linearly with the number of tools instead of quadratically.