Sub topic 7
Writing a partitioned dataset with write_dataset
This chapter assumes you can build a pyarrow.Table, know that Parquet stores data in row groups and column chunks, and understand that a partitioned dataset encodes column values in directory names. It covers how ds.write_dataset turns one table into a directory tree.
What write_dataset does
ds.write_dataset takes data and splits its rows by the partition columns. Each distinct combination of values gets its own directory, and the rows for that combination are written into one or more files inside it.
A minimal example
The partitioning is declared as a schema, which fixes the name and type of each partition column. The flavor="hive" argument produces name=value directory names.
import pyarrow as pa
import pyarrow.dataset as ds
table = pa.table({
"year": [2023, 2023, 2024, 2024],
"country": ["DE", "FR", "DE", "FR"],
"amount": [10.5, 20.0, 7.25, 3.0],
})
part = ds.partitioning(
pa.schema([("year", pa.int64()), ("country", pa.string())]),
flavor="hive",
)
ds.write_dataset(
table,
"sales",
format="parquet",
partitioning=part,
existing_data_behavior="overwrite_or_ignore",
)
# Creates paths such as sales/year=2023/country=DE/part-0.parquetThe order of fields in the schema sets the nesting order of the directories. Here year is the outer level and country the inner level.
The partition columns are not stored inside the Parquet files. Their values live only in the directory names, and the reader reconstructs the columns when it opens the dataset. The files contain just amount.
The arguments that matter
base_dir(second argument): the root directory. Local paths and remote file systems both work.format:"parquet"here. Other formats such as"ipc"and"csv"are also accepted.partitioning: ads.partitioning(...)object, or a list of column names combined withpartitioning_flavor="hive".basename_template: the file name pattern. The default ispart-{i}.parquet, where{i}is a counter.existing_data_behavior: what to do when the target already contains data.
Common errors
ArrowInvalid about existing data. The default for existing_data_behavior is "error". If the target directory already holds files, the write fails. Pick a different value on purpose:
| Value | Effect |
|---|---|
| `error` | Fail if matching data exists |
| `overwrite_or_ignore` | Write files, replacing any with the same name and leaving others alone |
| `delete_matching` | Delete the partition directories being written, then write |
Duplicate rows after a rerun. With overwrite_or_ignore, a rerun that produces differently named files leaves the old files in place. Use a fixed basename_template, or delete_matching, if you need a clean replacement.
Partition column not found. Every name in the partitioning must exist in the table's schema. A typo or a mismatched type raises an error at write time.
Exercise
You run the example above twice with the default existing_data_behavior. What happens on the second run, and which setting makes it replace only the partitions it writes?
Show answer
The second run fails with an error, because the default is "error" and the "sales" directory already contains data. Setting existing_data_behavior="delete_matching" deletes and rewrites only the partition directories that receive rows in the new write.