Sub topic 3
Inside an array: buffers, validity bitmaps and offsets
This chapter assumes you know basic Python, can read the pyarrow examples from earlier chapters, and understand that Arrow stores each column as one contiguous run of values. Here you look inside a single array and see which memory buffers it uses.
An array is a few buffers
An Arrow array is not a Python list of objects. It is a length, a type, and a small set of buffers. A buffer is a contiguous block of bytes. Which buffers an array has depends on its type.
| Type kind | Buffers, in order |
|---|---|
| Fixed-width (int32, float64) | validity bitmap, values |
| Variable-width (string, binary) | validity bitmap, offsets, data |
import pyarrow as pa
arr = pa.array([1, None, 3], type=pa.int32())
print(arr.null_count)
print(len(arr.buffers()))This prints 1 and then 2. The array has one null, and it holds two buffers: the validity bitmap and the values.
The validity bitmap
The validity bitmap stores one bit per slot. A set bit means the value is present. A cleared bit means the slot is null. Bits are packed least-significant-bit first, so slot 0 is the lowest bit of the first byte.
Because nulls live in the bitmap, the values buffer needs no sentinel such as NaN. The bytes in a null slot are undefined, and you should not read them.
import pyarrow as pa
arr = pa.array([1, None, 3], type=pa.int32())
bitmap = arr.buffers()[0]
print(bin(bitmap.to_pybytes()[0]))This prints 0b101. Slots 0 and 2 are valid, and slot 1 is null.
If an array has no nulls, the bitmap may be absent. In that case buffers()[0] is None. Code that reads buffers directly must handle that case.
Offsets for variable-width values
Strings have different lengths, so the position of value i cannot be computed from i alone. Arrow adds an offsets buffer. Value i occupies the bytes from offsets[i] up to offsets[i + 1] in the data buffer. An array of N strings therefore has N + 1 offsets.
import numpy as np
import pyarrow as pa
arr = pa.array(["ab", None, "cde"])
validity, offsets, data = arr.buffers()
print(np.frombuffer(offsets, dtype=np.int32)[:4])
print(data.to_pybytes()[:5])This prints [0 2 2 5] and b'abcde'. The null slot has equal neighbouring offsets (2 and 2), so it takes zero bytes of data. The slice [:4] is there because Arrow may pad buffers beyond their logical length.
Standard string and binary types use 32-bit offsets, which limits the data buffer to about 2 GiB. The large_string and large_binary types use 64-bit offsets and remove that limit, at the cost of larger offsets buffers.
Common errors
- Reading a null slot's bytes. The values buffer holds undefined data there. Check the bitmap or use
null_countandis_null. - Assuming a bitmap exists. Arrays without nulls may have
Nonein place of the bitmap. - Ignoring
arr.offset. A sliced array shares the parent's buffers and records a starting offset. Raw buffer reads must add it. - Trusting buffer sizes. Padding makes
buffer.sizeequal to or larger than the logical data. Use the array length to know how much is meaningful. - Expecting a Python list's cost model. Indexing a single element creates a Python scalar object. Prefer compute functions for bulk work.
Exercise
A string array pa.array(["x", "yz", None, "w"]) has four slots. How many offsets does it have, and what are their values?
Show answer
It has five offsets (N + 1): 0, 1, 3, 3, 4. The values "x", "yz" and "w" take 1, 2 and 1 bytes, and the null slot takes none, so offsets 3 and 3 repeat.