Sub topic 17
Common pitfalls and how to debug them
This chapter assumes you have written and read a Hive-partitioned Parquet dataset with ds.write_dataset and ds.dataset. It also assumes you know what a filter expression and a scanner are. The examples reuse the sales dataset from earlier chapters, partitioned by year and country.
Most partitioned-dataset bugs fall into three groups: types that differ between what you wrote and what you read, filters that do not prune anything, and files that are too many, too small, or not meant to be there. All three can be checked by inspecting what PyArrow discovered, before you read any data.
A debugging order that works
Pitfall: inferred partition types
With partitioning="hive", PyArrow infers each partition type from the directory text. The value month=01 is typically inferred as an integer, so the leading zero is lost. A filter written as ds.field("month") == "01" then compares a string to an integer and fails or matches nothing.
import pyarrow as pa
import pyarrow.dataset as ds
month_schema = pa.schema([("month", pa.string())])
part = ds.partitioning(month_schema, flavor="hive")
table = pa.table({"month": ["01", "02", "02"], "amount": [1.0, 2.0, 3.0]})
ds.write_dataset(
table,
"events",
format="parquet",
partitioning=part,
existing_data_behavior="overwrite_or_ignore",
)
inferred = ds.dataset("events", format="parquet", partitioning="hive")
print(inferred.schema.field("month").type) # an integer type, usually int32
declared = ds.dataset("events", format="parquet", partitioning=part)
print(declared.schema.field("month").type) # stringThe fix is to pass a partitioning object built from a schema, as in declared. Use the same object for writing and reading so the two sides cannot drift apart.
Directory partitioning has a related problem. The paths hold only values, such as sales_dir/2023/, so without a schema the field gets a generic name like field0. A filter on year then raises an error about an unknown field.
Pitfall: filters that do not prune
A filter prunes files only when it refers to a partition column and compares it with a value of the right type. A filter on an ordinary column, such as amount, cannot skip directories. At most it can skip row groups inside files, using Parquet statistics.
To check whether pruning happens, ask the dataset which fragments survive the filter:
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
flt = ds.field("year") == 2024
kept = [f.path for f in dataset.get_fragments(filter=flt)]
print(len(kept), "of", len(dataset.files), "files survive the filter")
for path in kept:
print(path)If the count equals the total, the filter did not prune. Check three things: the column name, the type (compare dataset.schema with your literal), and whether the column is really a partition key. A filter passed to to_table is applied to rows as well, so the result can be correct while the scan stays slow. Only the fragment count shows the difference.
Pitfall: schema taken from the first file
By default, PyArrow infers the dataset schema by inspecting the first file it finds. If later files have extra columns, those columns are silently missing from the dataset. If later files use a different type for the same column, you get a cast error at read time, far from the cause.
Pass an explicit schema to make the contract visible:
import pyarrow as pa
import pyarrow.dataset as ds
part = ds.partitioning(
pa.schema([("year", pa.int64()), ("country", pa.string())]),
flavor="hive",
)
schema = pa.schema([
("year", pa.int64()),
("country", pa.string()),
("amount", pa.float64()),
])
dataset = ds.dataset(
"sales",
format="parquet",
schema=schema,
partitioning=part,
ignore_prefixes=[".", "_", "tmp_"],
)
print(dataset.schema)Columns that are absent from a file are returned as nulls. Columns with an incompatible type raise an error.
Pitfall: files you did not expect
Discovery walks the whole tree. By default it skips names that start with . or _, which hides markers such as _SUCCESS and _metadata. A temporary file with any other prefix is treated as data. A stray CSV or a half-written Parquet file makes the read fail with a message about the file format.
Always print sorted(dataset.files) when a read fails. Extend ignore_prefixes for your own temporary names, or write to a staging directory and move the finished files into place.
Pitfall: null partition values
Hive partitioning cannot put a null in a directory name. PyArrow writes a placeholder, by default __HIVE_DEFAULT_PARTITION__, and reads it back as null. Other tools may use a different placeholder, and your own data may contain the same text. Before writing, check the key columns with table.column("country").null_count. Replace nulls with an explicit value such as "unknown" if downstream tools disagree on the placeholder.
Pitfall: too many small files
Each distinct combination of partition values creates at least one file. High-cardinality keys such as user_id produce thousands of tiny files, and each one adds a metadata call and a file open. Repeated small appends have the same effect. A quick audit for local data:
import os
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
sizes = sorted(os.path.getsize(path) for path in dataset.files)
print(len(sizes), "files")
print("smallest:", sizes[0], "median:", sizes[len(sizes) // 2], "bytes")If most files are well under a few megabytes, reduce the number of partition keys or coarsen them (month instead of day). Compact the data with a read-then-rewrite that sets max_rows_per_file.
When you call write_dataset several times into the same root, the default file name template part-{i}.parquet repeats. With overwrite_or_ignore, a later write replaces the earlier file of the same name. Give each run a unique template:
import uuid
import pyarrow as pa
import pyarrow.dataset as ds
run_id = uuid.uuid4().hex[:8]
table = pa.table({"year": [2025], "amount": [4.5]})
ds.write_dataset(
table,
"sales_log",
format="parquet",
partitioning=["year"],
partitioning_flavor="hive",
basename_template=f"run-{run_id}-{{i}}.parquet",
existing_data_behavior="overwrite_or_ignore",
)The doubled braces in the f-string leave a literal {i} for PyArrow to fill in.
Pitfall: surprising existing-data behavior
The default existing_data_behavior="error" stops the write if the target already contains data. People often switch to delete_matching to get past the error. That option deletes everything inside each partition directory the write touches, not only files with matching names. With no partition columns, the "partition directory" is the root itself. Confirm the root path before using it, and prefer overwrite_or_ignore with unique file names when you only append.
Symptom reference
| Symptom | Likely cause | First check |
|---|---|---|
| Filter on `month == "01"` returns nothing | Partition type inferred as integer | `dataset.schema` |
| `field0` instead of a name | Directory partitioning read without a schema | Partitioning object |
| Query is slow but correct | Filter is not on a partition column | `get_fragments(filter=...)` count |
| New column missing | Schema inferred from the first file | Pass `schema=` |
| Format error on read | Stray file in the tree | `sorted(dataset.files)` |
| Old data vanished | `delete_matching` on a broad directory | Root and partition paths |
| Memory error on `to_table` | Full result does not fit in memory | Use `scanner.to_batches()` |
Exercise
A dataset has directories like events/day=2024-03-01/. A filter ds.field("day") == "2024-03-01" returns zero rows, but the files exist. What is the likely cause, and how do you confirm and fix it?
Show answer
With partitioning="hive", PyArrow infers a type for day, and it may infer a date or timestamp instead of a string, so the comparison with a string literal fails or never matches. Confirm by printing dataset.schema.field("day").type and the partition_expression of the fragments. Fix it by passing a partitioning object with pa.string() for day, or by comparing with a value of the inferred type.
Exercise
You append to a dataset every hour with write_dataset and the default settings except existing_data_behavior="overwrite_or_ignore". After a day, the files from earlier hours seem to be gone from the current partition. Why, and what two changes help?
Show answer
Every run writes part-0.parquet into the same partition directory, and overwrite_or_ignore replaces files with the same name, so each run overwrites the last. Give each run a unique basename_template, for example one that includes a run id and {i}. Also consider batching several hours into one write, or compacting later, so you do not create many small files.