Sub topic 8
Computing on Arrow Data with Compute Functions
This chapter assumes you can run Python with pyarrow installed. It also assumes you know what an Arrow array, a table and a null are from earlier chapters.
What compute functions are
pyarrow.compute, usually imported as pc, is a library of functions that work on whole arrays. Each function takes Arrow arrays or scalars and returns a new Arrow array or scalar. The loop over values runs in compiled C++ code, not in Python.
Because the functions read Arrow buffers directly, they need no conversion step. The data stays in Arrow form from input to output.
Arrays are immutable. A compute function never edits its input. It allocates a new array for the result.
A first example
import pyarrow as pa
import pyarrow.compute as pc
arr = pa.array([1, 2, None, 4])
print(pc.sum(arr))
print(pc.is_null(arr))pc.sum returns a scalar. Printing it shows <pyarrow.Int64Scalar: 7>, so the null was skipped. pc.is_null returns a boolean array: false, false, true, false.
How nulls behave
Compute functions use the validity bitmap to decide what to do with missing values. Two rules cover most cases.
- Aggregations such as
sum,minandmeanskip nulls by default. Passskip_nulls=Falseto get a null result if any input is null. - Element-wise functions such as
addandgreaterpropagate nulls. A null input gives a null output at that position.
import pyarrow as pa
import pyarrow.compute as pc
arr = pa.array([1, 2, None, 4])
print(pc.sum(arr, skip_nulls=False))
print(pc.add(arr, 10))
print(pc.fill_null(arr, 0))The first line prints a null Int64Scalar. The second gives 11, 12, null, 14. The third replaces the null with zero and gives 1, 2, 0, 4.
Filtering with a mask
A common pattern is to build a boolean mask with a comparison function, then pass the mask to filter.
import pyarrow as pa
import pyarrow.compute as pc
table = pa.table({"id": [1, 2, 3, 4], "name": ["a", "b", None, "d"]})
mask = pc.greater(table["id"], 1)
subset = table.filter(mask)
print(subset.num_rows)The result has three rows. table["id"] is a chunked array, which is a column made of one or more arrays. Compute functions accept chunked arrays and handle the chunks for you.
By default, filter drops rows where the mask is null, not only rows where it is false. If a comparison hits a null value, that row disappears from the result.
Common errors
- Type mismatch. Comparing an integer column with a string raises
ArrowNotImplementedError. Cast first withpc.castor build the scalar with the right type. - Expecting Python scalars.
pc.sumreturns apyarrowscalar. Call.as_py()to get a normal Python value. - Looping in Python. Calling a compute function once per element throws away the speed advantage. Pass the whole array.
- Assuming in-place change. The input array is never modified, so assign the result to a name.
Where compute functions fit
Compute functions are a set of building blocks, not a query engine. They run on data already in memory. The dataset scans in a later chapter use the same functions to apply filters while reading files.
Exercise
Given arr = pa.array([5, None, 7]), what does pc.sum(arr, skip_nulls=False) return, and what does pc.greater(arr, 5) return?
Show answer
The sum is a null Int64Scalar, because skipping nulls is turned off and one input is null. pc.greater(arr, 5) returns the boolean array false, null, true. The null input gives a null output, and 5 is not greater than 5.