numbarrow.core.adapters

Overview

numba cannot compile a function that takes a pyarrow.Array. arrow_array_adapter() converts such an array into a pair of numpy arrays, (bitmap, data), that an @njit function can take: the validity bitmap that marks the null rows, and the values. Call it outside jitted code, once per array, and pass the results to the jitted function:

import pyarrow as pa
from numbarrow.core.adapters import arrow_array_adapter

bitmap, data = arrow_array_adapter(pa.array([1.5, None, 4.0]))
print(bitmap, data)   # [5] [1.5 0.  4. ]

bitmap is a uint8 array with one bit per row, set for a valid row. In the example, [5] is 0b101: rows 0 and 2 are valid, and row 1 is null. bitmap is None when the array has no validity buffer, the Arrow memory that marks its nulls, so code that reads a bitmap must accept None as well as an array. data is a read-only numpy array. For a fixed-width type such as int64, float64 or a timestamp, it is a view of the Arrow buffer, and it keeps that buffer alive. For booleans, date32 values and strings, which numpy stores differently, it is a copy.

A StructArray converts to a 3-tuple instead: the struct’s own bitmap, then two dicts keyed by field name, one of field bitmaps and one of field data. A ListArray whose elements are structs converts to the same 3-tuple, over the elements of all its rows, and its first item marks null elements rather than null rows. The adapter keeps the struct’s bitmap apart from the field bitmaps, so pass both to is_null_struct(). make_mapinarrow_func() combines them for the function it calls instead.

The function accepts the array classes that the README lists, and their subclasses. A MapArray, for example, is a ListArray of structs with the fields key and value, and it converts as one. Any other array, and any object that is not a pyarrow array, raises NotImplementedError with the type in the message.

The README’s Supported Types section has the table of classes and what each becomes. Its subsections cover what code that reads the results must handle: nulls and validity bitmaps, strings, timestamps and time zones, structs, lists of structs and maps, read-only arrays and explicit signatures.

Module

The adapters that turn pyarrow arrays into numpy arrays for numba-compiled code.

arrow_array_adapter() takes a pyarrow array and returns its validity bitmap and its values as numpy arrays, which an @njit function can take as arguments. It dispatches on the class of the array, with functools.singledispatch(), to a handler registered in this module. The values of an int32, int64, float64, uint8, date64 or timestamp array come back as a view of the Arrow buffer. Booleans, date32 values and strings are copied, because numpy lays them out differently. cast_64bit_date_arrow_to_numpy_array() is the datetime64 view that the date64 and timestamp handlers share, and numbarrow.utils.arrow_array_utils holds the functions that adapt numeric, string, struct and list arrays.

numbarrow.core.adapters.arrow_array_adapter(pa_array: Array)[source]
numbarrow.core.adapters.arrow_array_adapter(pa_array: BooleanArray)
numbarrow.core.adapters.arrow_array_adapter(pa_array: Date32Array)
numbarrow.core.adapters.arrow_array_adapter(pa_array: Date64Array)
numbarrow.core.adapters.arrow_array_adapter(pa_array: DoubleArray | Int32Array | Int64Array | UInt8Array)
numbarrow.core.adapters.arrow_array_adapter(pa_array: DoubleArray | Int32Array | Int64Array | UInt8Array)
numbarrow.core.adapters.arrow_array_adapter(pa_array: DoubleArray | Int32Array | Int64Array | UInt8Array)
numbarrow.core.adapters.arrow_array_adapter(pa_array: DoubleArray | Int32Array | Int64Array | UInt8Array)
numbarrow.core.adapters.arrow_array_adapter(pa_array: ListArray)
numbarrow.core.adapters.arrow_array_adapter(pa_array: StructArray)
numbarrow.core.adapters.arrow_array_adapter(pa_array: StringArray | LargeStringArray)
numbarrow.core.adapters.arrow_array_adapter(pa_array: StringArray | LargeStringArray)
numbarrow.core.adapters.arrow_array_adapter(pa_array: TimestampArray)

Return the validity bitmap and the values of a pyarrow array as numpy arrays for numba-compiled code.

The function dispatches on the class of pa_array, and a subclass of a registered class takes that class’s handler: a MapArray adapts as the ListArray of key and value structs that it is. For the following classes, the result is (bitmap, data):

  • An Int32Array, Int64Array, DoubleArray or UInt8Array gives a view of its values.

  • A Date64Array or TimestampArray gives a datetime64 view at its own unit, ms for a date64. A timestamp’s time zone is dropped: the values are the UTC instants that Arrow stores, as pyarrow’s to_numpy returns them.

  • A BooleanArray gives a copy as bool_, and a Date32Array a copy as datetime64[D].

  • A StringArray or LargeStringArray gives a copy as a fixed-width numpy unicode array, as many characters wide as its longest non-null value, and at least one. The width differs from one array to the next, as between two batches of a Spark column, so compile a numba function that takes the strings lazily, without a signature.

For a StructArray, and for a ListArray whose elements are structs, the result is (struct_bitmap, bitmaps, datas): the struct’s own bitmap, then two dicts that map each field’s name to the field’s bitmap and to its values, each adapted as above. A field’s bitmap need not mark the struct’s null rows, so pass it with struct_bitmap to is_null_struct(). For a list array, the values are the elements of all its rows in order, without the offsets that separate the rows.

A bitmap is None if the array has no validity buffer, and every value is then valid. Otherwise it is a writable uint8 array that owns its memory, with the bit of element 0 first, even for a slice. An array without nulls can still have a validity buffer: a slice, take or filter of an array that has one keeps it. An array read from Arrow IPC, the format of Spark’s mapInArrow batches, has none if it has no nulls. A numba signature should therefore declare a bitmap parameter Optional.

Every data array is read-only, a copy as well as a view, so declare it with readonly=True in a numba signature. A view keeps the Arrow buffer that it reads alive.

Any other object raises NotImplementedError with a message that names its type. A ChunkedArray, such as a Table column, and a pyarrow scalar raise it too, with a message that says what to pass instead. The functions that adapt strings, structs and lists refuse some values, such as a string that ends in a NUL character or a list array whose rows differ in length. create_str_array(), structured_array_adapter() and structured_list_array_adapter() list the values they refuse.

numbarrow.core.adapters.cast_64bit_date_arrow_to_numpy_array(pa_array: Array, np_dtype: dtype)[source]

Return the bitmap of a date64 or timestamp array and a datetime64 view of its values, without a copy.

Both types hold one int64 per value, as datetime64 does, so the function views the Arrow buffer. np_dtype must be datetime64 at the array’s own unit: ms for a date64, and the unit of the type for a timestamp. A view cannot rescale the values, so any other np_dtype raises ValueError. So does an array of another type, a time64 or a duration included. The view is read-only, and the bitmap is None if the array has no validity buffer.