Sub topic 6
Declaring Partition Schemas and Types
This chapter assumes you can write and read a basic Hive-partitioned Parquet dataset with ds.write_dataset and ds.dataset. It also assumes you know what an Arrow schema is. Here you learn how to tell PyArrow the name and type of each partition column.
Why partition types need declaring
Partition values live in directory names, not inside the Parquet files. A path like sales/year=2023/country=DE/ is just text. PyArrow must turn that text into typed columns.
The two flavors differ in how much help they give:
- Hive partitioning has
name=valuesegments. PyArrow can read the names from the path. If you give no schema, it guesses types from the values, soyear=2023becomes an integer andcountry=DEbecomes a string. - Directory partitioning has only values, such as
sales_dir/2023/. The path carries no names, so you must supply a schema to name each level.
Guessing is convenient but fragile. A value such as 007 can become the integer 7. A partition that holds only digits in one directory and letters in another can fail to unify. Declaring the schema removes the guesswork.
Declaring a partition schema
Build the schema with pa.schema, then pass it to ds.partitioning. For directory layouts, field order matters because it maps to directory depth.
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({"year": [2023, 2024], "amount": [1.0, 2.0]})
part = ds.partitioning(pa.schema([("year", pa.int16())]))
ds.write_dataset(
table,
"sales_dir",
format="parquet",
partitioning=part,
existing_data_behavior="overwrite_or_ignore",
)
dataset = ds.dataset("sales_dir", format="parquet", partitioning=part)
print(dataset.to_table().schema)The year column comes back as int16, not int64, because the partition schema set that type. Smaller integer types are fine when the range is known.
For Hive layouts, add flavor="hive":
import pyarrow as pa
import pyarrow.dataset as ds
part = ds.partitioning(
pa.schema([("year", pa.int64()), ("country", pa.string())]),
flavor="hive",
)
dataset = ds.dataset("sales", format="parquet", partitioning=part)
print(dataset.schema)Discovery versus a fixed schema
You can also ask PyArrow to infer a Hive schema with partitioning="hive". A declared schema differs in three ways:
| Approach | Types | Names | Risk |
|---|---|---|---|
| `partitioning="hive"` | Inferred from values | From the path | Inference may pick the wrong type |
| `ds.partitioning(schema, flavor="hive")` | You choose | From your schema | A value that does not fit the type causes an error |
| `ds.partitioning(schema)` (directory) | You choose | From your schema | Field order must match directory depth |
Dictionary-encoded partitions
For string columns with few distinct values, you can ask for dictionary types. Pass infer_dictionary=True to ds.partitioning when you want discovered string values stored as dictionaries, which saves memory on large scans.
Exercise
You wrote a directory-partitioned dataset with ds.partitioning(pa.schema([("year", pa.int16())])). Why must you pass the same partitioning when reading it back, and what happens to the column type?
Show answer
Directory names contain only values such as 2023, so PyArrow has no column name or type without a schema. Passing the same partitioning gives the column the name year and the type int16. Without it, the partition values are not exposed as a column.