Sub topic 1
Why data tools need a shared in-memory format
Modern data work rarely stays inside one tool. A pipeline might read files with one library, clean them in pandas, run a SQL query in a database engine, and hand the result to a machine learning framework. Each hand-off moves a table from one program's memory into another's. This chapter explains why that hand-off is expensive and what a shared in-memory format changes. Later chapters cover how Arrow does it.
Prerequisites
This chapter assumes you have:
- Basic Python, including installing packages with pip.
- Familiarity with tables, rows and columns, for example from pandas or SQL.
- A basic idea of what memory and files are, and the difference between in-memory and on-disk data.
To run the example, install the library with pip install pyarrow.
The interchange problem
Every data tool keeps tables in its own internal layout. pandas stores columns as NumPy arrays plus Python objects for strings. A database engine uses its own page and vector structures. A Java service might hold rows as objects. These layouts are tuned for each tool's needs, but they differ in details that matter:
- How a missing value is marked (a sentinel such as
NaN, a separate flag, or a bitmap). - How text is stored (Python string objects, or one block of bytes plus positions).
- Whether values are grouped by row or by column.
- How wide each integer or timestamp is, and what its unit is.
When two tools disagree on these details, the data must be converted. The usual route is to serialize the table, which means turning it into a sequence of bytes. Then the other tool deserializes it, parsing those bytes back into its own layout. CSV and JSON are common examples. Both steps touch every value.
What the conversion costs
Serialization cost shows up in four places:
- CPU time. Formatting a float as text and parsing it back is far slower than copying 8 bytes.
- Memory. The sender holds the original, the intermediate bytes exist, and the receiver holds its copy. Peak memory can be two or three times the data size.
- Lost information. CSV has no types. A column of integers with gaps may come back as floats. Timestamps lose their time zone. A string column of digits may lose leading zeros.
- Latency. A downstream step cannot start until the conversion finishes.
The loss of type information deserves attention, because it causes bugs rather than just slowness. Two programs that exchange a CSV file must agree, outside the file, on what each column means.
The N×M problem
Suppose there are N data systems and each needs to exchange data with all the others. If every pair needs a custom converter, you need roughly N × (N − 1) converters. Each converter is code that someone has to write, test, optimize and keep in step with changes in both tools. Ten systems would need up to 90 of them.
A shared format changes the count. Each system needs one reader and one writer for the common format, so the total is about 2 × N. The diagram shows the difference.
This is the same idea as a lingua franca, or a universal power adapter. Apache Arrow is that shared format for tabular data in memory.
What Arrow is
Apache Arrow is a specification for how a table is laid out in memory, plus libraries that implement it in many languages (C++, Python, Java, Rust, Go, R and others). Three properties make it useful as a common ground.
- Language independent. The layout is defined in terms of bytes and offsets, not objects from one language. A buffer built in C++ means the same thing to a Rust program that receives it.
- Columnar. Values from one column sit next to each other in memory. This suits analytics, where queries usually scan a few columns across many rows, and it suits modern CPUs, which process contiguous runs of numbers quickly. The next chapter covers this in detail.
- Typed and self-describing. Every table carries a schema, which is a list of column names and exact types. Nothing needs to be agreed outside the data.
The key point is that Arrow is an in-memory format meant to be used directly. A tool does not have to translate Arrow data into its own layout before working on it. It can compute on the Arrow buffers as they are. When two tools both understand Arrow, handing over a table can be as cheap as handing over a pointer to memory the receiver can already read.
A first look at the cost
The following program builds a table of one million rows. It converts the table to a list of Python dictionaries, which is the kind of conversion tools do when they lack a common format. It then writes the same table to Arrow's own byte format and compares the time and size. The IPC format used here is covered in a later chapter, so for now only the comparison matters.
import time
import pyarrow as pa
n = 1_000_000
table = pa.table(
{
"id": pa.array(range(n), type=pa.int64()),
"score": pa.array([i * 0.5 for i in range(n)], type=pa.float64()),
}
)
start = time.perf_counter()
rows = table.to_pylist()
python_seconds = time.perf_counter() - start
start = time.perf_counter()
sink = pa.BufferOutputStream()
with pa.ipc.new_stream(sink, table.schema) as writer:
writer.write_table(table)
buf = sink.getvalue()
ipc_seconds = time.perf_counter() - start
print(f"to Python dicts: {python_seconds:.3f} s, {len(rows)} objects")
print(f"to Arrow IPC: {ipc_seconds:.3f} s, {buf.size} bytes")Exact timings depend on your machine. In a typical run the Python conversion takes on the order of a second, because it creates millions of Python objects. The IPC write takes a few milliseconds, because the data is already in the layout that gets written, and the library copies the buffers rather than formatting each value. The IPC output is also about 16 MB, which is the size of two columns of eight-byte numbers, with only a small amount of metadata added.
Where a shared format fits in a pipeline
Consider a pipeline that reads Parquet files, filters them with a query engine, and passes the result to pandas for plotting. Without a shared format, each stage boundary involves a conversion. With Arrow, the Parquet reader decodes into Arrow memory, the query engine operates on that same memory, and pandas can often read from it directly or with a limited conversion.
Arrow also helps across process and machine boundaries, not just inside one process. Because the layout is the same everywhere, a table can be sent over a socket or stored in a file in nearly the same form it has in memory. Later chapters cover the IPC format, the C data interface for sharing within a process, and Arrow Flight and ADBC for network and database transfer.
What a shared format does not solve
Arrow reduces conversion cost. It does not remove every cost, and some problems sit outside its scope.
- It is not a storage format for long-term archival. Arrow describes memory layout. Compressed, column-oriented storage on disk is the job of formats such as Parquet, and the two work together.
- Not every conversion is free. Some types have no exact match in other tools. pandas uses
NaNfor missing floats and has no bitmap for them, so a conversion may have to build or inspect one. Strings stored as Python objects must be copied into contiguous bytes. Later chapters show which paths are zero-copy and which are not. - It does not make slow code fast. If a pipeline is dominated by network latency or an inefficient query, a faster interchange step changes little.
- Both sides must adopt it. The savings appear only when the tools on either side of the hand-off speak Arrow. Where one side does not, you still pay for a conversion at that boundary.
- Row-by-row workloads gain little. Arrow favors operating on whole columns. Code that handles one record at a time, such as a transactional service, gets less benefit.
Summary
Data tools lose time and memory, and sometimes type information, whenever they convert tables between internal layouts. Pairwise converters multiply as the number of tools grows. A shared in-memory format cuts that to one reader and one writer per tool. Apache Arrow provides such a format: language independent, columnar, and carrying its own schema. The next chapter looks at what columnar layout means and why it suits analytic work.
Exercise
A team has five data tools and needs every one to exchange tables with each of the other four, using custom converters in both directions. Roughly how many converters do they need? How many do they need if all five tools adopt a shared format with one reader and one writer each? Name one reason the shared format may still not remove all conversion cost.
Show answer
Pairwise converters in both directions need 5 × 4 = 20. With a shared format, each tool needs one reader and one writer, so 5 × 2 = 10 components. The shared format may still leave conversion cost where a tool's internal types do not match it. For example, pandas stores missing floats as NaN rather than in a validity bitmap, and Python string objects must be copied into contiguous bytes. A tool that does not support the shared format at all also forces a conversion at that boundary.
Exercise
In the timing example, why is writing the table to the Arrow IPC stream so much cheaper than calling to_pylist()?
Show answer
to_pylist() creates one Python object per value, about two million here, and each needs allocation and type conversion. The IPC write starts from data that already has the Arrow layout, so the library writes the schema and copies the existing column buffers. It does no per-value formatting or object creation.