Sub topic 4
Data types and schemas
This chapter assumes you know basic Python, can install packages with pip, and understand the idea of columns and rows from pandas or SQL. You should also know from the previous chapters that Arrow stores each column as buffers: a validity bitmap, optional offsets, and values.
Why types are explicit
Arrow data is not self-describing at the byte level. A buffer of 12 bytes could be three int32 values, one int32 and one int64, or twelve characters. The data type tells a reader how to interpret the buffers. The schema lists the name and type of every column in a table.
Because every Arrow implementation agrees on these types, a reader in another language can interpret your buffers without guessing.
Common data types
| Type | pyarrow constructor | Buffers used |
|---|---|---|
| 32-bit integer | `pa.int32()` | validity, values |
| 64-bit integer | `pa.int64()` | validity, values |
| 64-bit float | `pa.float64()` | validity, values |
| UTF-8 string | `pa.string()` | validity, offsets, data |
| Boolean | `pa.bool_()` | validity, bit-packed values |
| Timestamp | `pa.timestamp("us")` | validity, values |
| List | `pa.list_(pa.int32())` | validity, offsets, child values |
Fixed-width types such as integers and floats store one value after another. Variable-width types such as strings need an offsets buffer to mark where each value starts and ends.
Types can also be nested. A list<int32> column holds a list of integers in each row. A struct type holds several named fields in each row.
Defining a schema
A schema is built from a list of fields. Each field has a name, a type, and a nullable flag.
import pyarrow as pa
schema = pa.schema([
("id", pa.int64()),
("name", pa.string()),
])
table = pa.table({"id": [1, 2, 3], "name": ["a", None, "c"]}, schema=schema)
print(table.schema)
print(table.num_rows)The schema fixes id as a 64-bit integer and name as a string. The None in name becomes a null in the validity bitmap. Without the schema argument, pyarrow would infer the types from the Python values.
Inference and its pitfalls
Inference is convenient, but it picks defaults that may not match what you want. Python integers become int64, and Python floats become double. If you need int32, say so.
import pyarrow as pa
inferred = pa.array([1, 2, 3])
explicit = pa.array([1, 2, 3], type=pa.int32())
print(inferred.type)
print(explicit.type)This prints int64 and then int32. The two arrays hold the same numbers but use different amounts of memory, and they are different types when you compare schemas.
A common error is passing values that do not fit the declared type. Asking for pa.int8() with the value 1000 raises pyarrow.lib.ArrowInvalid. Mixing text and numbers in one column without a schema raises a similar error, because no single type fits both.
import pyarrow as pa
try:
pa.array([1, "two", 3])
except pa.ArrowInvalid as err:
print("conversion failed")Schemas must match
Schemas matter when you combine data. Two tables can be concatenated only if their schemas agree. A column that is int32 in one table and int64 in the other will fail, so cast first.
import pyarrow as pa
a = pa.table({"x": pa.array([1, 2], type=pa.int32())})
b = pa.table({"x": pa.array([3, 4], type=pa.int64())})
b_cast = b.cast(a.schema)
combined = pa.concat_tables([a, b_cast])
print(combined.num_rows)The same rule applies when you write IPC or Parquet files and when other tools read them. The schema travels with the data, so a mismatch is caught early instead of producing wrong numbers.
Exercise
You build pa.array([1, 2, 3]) and later concatenate its table with a table whose column is int32. The concatenation fails. Why does it fail, and how do you fix it?
Show answer
Python integers are inferred as int64, so the two schemas differ. Cast one table to the other's schema, for example b.cast(a.schema), or declare type=pa.int32() when you build the first array.
Exercise
Which buffers does a nullable string array use, and why does it need more than a nullable int32 array?
Show answer
It uses a validity bitmap, an offsets buffer and a data buffer. Strings have different lengths, so the offsets mark where each value starts and ends. An int32 array needs only the validity bitmap and the values buffer because every value has the same width.