Sub topic 8
Opening a dataset and inspecting what was discovered
This chapter assumes you can read basic Python and know what a Parquet file is. It also assumes you have read the earlier chapters on how partition values appear in directory names and how write_dataset creates them. Here you open that directory tree again and look at what PyArrow found.
What `ds.dataset` does
ds.dataset(path, format=..., partitioning=...) does not load data. It runs discovery:
- It lists the files under
path. - It skips files whose names start with an ignored prefix.
- It parses each file's directory segments into partition values.
- It reads the schema of the first file it finds.
- It combines the file schema and the partition fields into one dataset schema.
Each discovered file becomes a fragment. A fragment is a unit of scanning, and for Parquet it is one file. Row data and Parquet row-group statistics are not read at this point, so opening a dataset with thousands of files is cheap compared with reading them.
Create data to inspect
The script below writes the same small table used earlier, so you can run every later snippet against it. It uses overwrite_or_ignore, so you can run it repeatedly.
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",
)Open the dataset and read the schema
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
print(dataset.schema)
print(sorted(dataset.files))The string "hive" asks PyArrow to discover the partition fields from the key=value directory names. The schema lists the columns stored inside the files first (amount), then the partition fields (year, country). The partition columns are not stored in the Parquet files, because write_dataset removes them. The values exist only in the paths.
dataset.files returns the file paths found by discovery. It is a good first check when a dataset seems empty or too large. Compare the list with what you expect to see on disk.
Inferred types differ from written types
You wrote year as int64, but discovery has only directory names such as year=2023 to work from. It infers int32 for integer-looking values and string for everything else. So the schema you get back may not match the table you wrote.
If you want exact types, pass a partitioning with a schema:
import pyarrow as pa
import pyarrow.dataset as ds
typed = ds.partitioning(
pa.schema([("year", pa.int16()), ("country", pa.string())]),
flavor="hive",
)
dataset = ds.dataset("sales", format="parquet", partitioning=typed)
print(dataset.schema.field("year").type) # int16Declared types matter later. Filters on year are compared against this type, and a mismatch can raise an error or silently prevent a value from matching.
Inspect fragments
A dataset is a collection of fragments. Each fragment knows its path and the partition values that apply to it.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
for fragment in dataset.get_fragments():
print(fragment.path)
print(" partition:", fragment.partition_expression)
print(" rows:", fragment.count_rows())partition_expression is a filter expression, such as one that says year == 2023 and country == "DE". It is built from the path alone, with no file read. This is the property that makes partition pruning cheap. The scanner can compare your filter with each fragment's expression and discard fragments that cannot match before it opens them.
For Parquet fragments you can read more, but each of these touches the file footer:
| Attribute or method | What it gives you | Reads the file? |
|---|---|---|
| `fragment.path` | The file path | No |
| `fragment.partition_expression` | Expression derived from the path | No |
| `fragment.physical_schema` | The schema stored in that file | Yes, the footer |
| `fragment.metadata` | Parquet metadata, including row-group statistics | Yes, the footer |
| `fragment.count_rows()` | Row count, from footer metadata where possible | Yes, the footer |
dataset.count_rows() does the same across all fragments. It is much cheaper than loading a table and calling num_rows, because it uses footer metadata and reads no column data.
What discovery ignores
By default ds.dataset ignores any file or directory whose name starts with . or _. This hides markers such as _SUCCESS and hidden files. You can change this with ignore_prefixes:
import pyarrow.dataset as ds
dataset = ds.dataset(
"sales",
format="parquet",
partitioning="hive",
ignore_prefixes=[".", "_", "tmp_"],
)Note that this list replaces the default, so you must repeat . and _ if you still want them ignored.
With Hive discovery, directory segments that are not of the form key=value do not produce partition fields. A stray directory such as backup/ does not produce an error. Files inside it are still discovered and appear in the dataset with no value for that level, which can lead to missing partition values in your results. If such a directory should not be read, point ds.dataset at a narrower path or list the files explicitly.
You can also pass a list of file paths instead of a directory. Use partition_base_dir to tell PyArrow where the partition-bearing part of those paths begins:
import pyarrow.dataset as ds
files = [
"sales/year=2024/country=DE/part-0.parquet",
"sales/year=2024/country=FR/part-0.parquet",
]
dataset = ds.dataset(
files,
format="parquet",
partitioning="hive",
partition_base_dir="sales",
)
print(dataset.to_table().to_pydict())Schema choices at open time
The dataset schema comes from the first file by default. If later files have extra columns or different types, the dataset does not notice during discovery, and the problem appears only when those files are scanned. Two defences exist:
- Pass
schema=tods.datasetto declare the schema you expect. Files are then read against it, and columns missing from a file come back as nulls. - Check
fragment.physical_schemaacross fragments, which is slow for many files but exact.
Debugging checklist
When a dataset does not look right, check these in order:
- Print
dataset.files. Are all the expected files there, and no extra ones? - Print
dataset.schema. Do the partition fields exist, with the types you expect? - Print each fragment's
partition_expression. Do the values match the directory names? - Print
dataset.count_rows()and compare it with the number of rows you wrote.
Exercise
You open a dataset with ds.dataset("sales", format="parquet") and no partitioning argument. The schema shows only "amount", and filtering on year raises an error. What went wrong, and what is the smallest change that fixes it?
Show answer
Without a partitioning argument, discovery does not parse the year=... and country=... directory names, so the partition fields are not part of the schema. The fix is to add partitioning="hive" (or a ds.partitioning(schema, flavor="hive") object if you need specific types), so year and country appear in the schema and can be used in filters.