Sub topic 1
Refresher: Arrow tables and schemas
Prerequisites
This chapter assumes you can read basic Python and know what a table with rows and columns is, for example from pandas or SQL. You should also know roughly what a Parquet file is. It reviews only the Arrow ideas that the later chapters on partitioned datasets rely on.
Why start here
A partitioned dataset is a set of files that PyArrow presents as one table. That table needs a schema, which is the list of column names and types. The schema decides how values are read, how filters compare them, and how partition values in directory names are interpreted.
Tables, arrays and schemas
A pyarrow.Table is a two-dimensional, column-oriented structure. Each column is a ChunkedArray, a sequence of one or more Array pieces of the same type. Each column has a name and a type, such as int64, string or float64. A Schema is the ordered collection of these fields.
import pyarrow as pa
schema = pa.schema([
("year", pa.int64()),
("country", pa.string()),
("amount", pa.float64()),
])
table = pa.table(
{
"year": [2023, 2023, 2024, 2024],
"country": ["DE", "FR", "DE", "FR"],
"amount": [10.5, 20.0, 7.25, 3.0],
},
schema=schema,
)
print(table.schema)
print(table.num_rows, table.column_names)
print(table.column("amount").type)If you omit schema=, Arrow infers types from the Python values. Here it would infer int64, string and double. Passing the schema explicitly makes your intent clear and avoids surprises.
Types are fixed per column
Every value in a column has the same type, and any value can be null. Arrow stores nulls in a separate validity bitmap, so an integer column can contain nulls without turning into floats, as it would in older pandas versions.
To change a type, cast the table to a new schema. The cast fails if a value does not fit the new type.
import pyarrow as pa
table = pa.table({"year": [2023, 2024], "amount": [1.5, 2.5]})
narrow = pa.schema([("year", pa.int16()), ("amount", pa.float32())])
small = table.cast(narrow)
print(small.schema)Why this matters for partitioning
Later chapters put the values of columns such as year and country into directory names. When PyArrow reads those directories back, it must turn the text 2024 into a typed value. The schema you declare for the partition columns decides whether that value becomes an int16, an int64 or a string. Filters rely on the same types. Comparing year == 2024 only works as expected if year is a number and not a string.
The table below lists the types used in this tutorial.
| Arrow type | Python value | Typical use |
|---|---|---|
| `pa.int64()` | `2024` | Years, counts, IDs |
| `pa.int16()` | `2024` | Compact partition keys |
| `pa.string()` | `"DE"` | Country codes, categories |
| `pa.float64()` | `10.5` | Measures such as amounts |
Exercise
Starting from the table in the first example, which call gives you a new table in which year is int16, and does the original table change?
Show answer
Call table.cast(pa.schema([("year", pa.int16()), ("country", pa.string()), ("amount", pa.float64())])). The schema passed to cast must have the same field names as the table. The original table does not change, because Arrow tables are immutable and cast returns a new table.