Sub topic 6
Zero-copy sharing and who owns the memory
This chapter assumes you can read basic pyarrow code and know what an Arrow array's buffers are (a validity bitmap plus a data buffer). It also assumes you know the difference between a copy and a view of the same memory.
What zero-copy means
Zero-copy means a second object reads the same bytes as the first one, instead of getting its own duplicate. Arrow can do this because its buffers have a fixed, documented layout. Any library that understands the layout can point at the bytes directly.
The saving is real for large data. A view costs a small, fixed amount of work no matter how many values it covers. A copy costs time and memory in proportion to the data.
Who owns the memory
A buffer is a block of memory with a lifetime. Something must free it, and that something is the owner. When a view shares a buffer, the buffer stays alive until every holder has let go. In pyarrow this is reference counting. The NumPy array you get back holds a reference to the Arrow buffer, so the memory is not freed while the NumPy array exists.
Views are read-only in practice. Arrow buffers are treated as immutable, so a shared view should not be written to. If one holder could change the bytes, the other holders would see the change without warning.
A zero-copy conversion
A primitive array without nulls can be viewed as a NumPy array directly.
import numpy as np
import pyarrow as pa
values = np.array([1, 2, 3], dtype=np.int64)
arr = pa.array(values)
back = arr.to_numpy(zero_copy_only=True)
print(back)The zero_copy_only=True argument is a safety check. If a view is not possible, pyarrow raises an error instead of copying quietly. Without it, a conversion that copies looks the same as one that does not, and you only find out later from memory use or run time.
When a copy is unavoidable
Zero-copy needs the two sides to agree on the bytes. Several common cases break that agreement.
| Situation | Why a view is not possible |
|---|---|
| The array has nulls | NumPy integer arrays have no validity bitmap, so the nulls cannot be represented |
| String or binary data | Arrow uses an offsets buffer plus a data buffer, and NumPy has no matching layout |
| Boolean data | Arrow packs booleans as bits, while NumPy stores one byte each |
| Chunked data | A column made of several chunks is not one contiguous run of memory |
Here is the first case failing on purpose.
import pyarrow as pa
arr = pa.array([1, None, 3], type=pa.int64())
try:
arr.to_numpy(zero_copy_only=True)
except pa.ArrowInvalid as err:
print("cannot view:", err)Nulls are the most frequent surprise. A column that was zero-copy yesterday becomes a copy the day a single null appears in the data.
Slicing is also zero-copy
Slicing an array creates a new array object with an offset and a length into the same buffers. No values move.
import pyarrow as pa
arr = pa.array(range(1_000_000))
part = arr.slice(10, 5)
print(part.to_pylist())One consequence is that a small slice keeps the whole parent buffer alive. If you keep only a few rows from a very large array for a long time, copy them out so the large buffer can be freed.
Common errors
- Writing to a NumPy view of Arrow memory. The array is read-only by default, so the write raises an error. Make an explicit copy first.
- Expecting zero-copy for strings or nullable data. Check the table above.
- Holding tiny slices of huge arrays and wondering why memory never drops.
Exercise
You call column.to_numpy(zero_copy_only=True) on an integer column and it works in testing. In production it raises ArrowInvalid. Name two likely causes.
Show answer
The production column probably contains nulls, which NumPy integer arrays cannot represent without a copy. It may also be a chunked column made of several buffers, so it is not one contiguous run of memory. Either case needs a copy, for example by calling to_numpy(zero_copy_only=False).