Sub topic 8
Opening a dataset and inspecting what was discovered
This chapter assumes you can write a Hive-partitioned dataset with ds.write_dataset and know that a dataset is a set of files treated as one table. Here you open that dataset and check what PyArrow found before you run any query.
What `ds.dataset` does
ds.dataset walks the directory tree and lists the files it finds. It does not read the data. It builds one fragment per file and parses the partition values out of each path. Reading starts only when you call something like to_table or scanner.
Open and inspect
This example writes a small dataset first, so you can run it on its own.
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],
})
ds.write_dataset(
table,
"sales",
format="parquet",
partitioning=["year", "country"],
partitioning_flavor="hive",
existing_data_behavior="overwrite_or_ignore",
)
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
print(dataset.schema)
print(sorted(dataset.files))
for fragment in dataset.get_fragments():
print(fragment.path, fragment.partition_expression)The output shows three things:
- Schema:
amountcomes first, followed byyearandcountry. The partition columns are not stored inside the Parquet files, so PyArrow appends them to the file columns. - Files:
dataset.fileslists the paths found, such assales/year=2023/country=DE/part-0.parquet. - Partition expression: each fragment carries an expression such as
(year == 2023) and (country == "DE"). This expression is what later lets a filter skip the file without opening it.
Type inference
With partitioning="hive", PyArrow guesses the type of each partition field from the directory names. Integer-looking values become int32 and everything else becomes string. Your original year column was int64, so the type you read back differs from the type you wrote. To control the type, pass a schema, as in ds.partitioning(pa.schema([...]), flavor="hive").
What discovery ignores
By default, discovery skips files whose names start with . or _. Parquet writers often create markers such as _SUCCESS or _metadata, and this rule keeps them out of your data. You can change the rule with the ignore_prefixes argument.
Quick checks after opening
- Print
dataset.schemaand confirm the partition columns are present with the types you expect. - Compare
len(dataset.files)with the number of files you expect. - Look at the fragments'
partition_expressionvalues. If one istrue, that file's path did not match the partitioning you declared.
Exercise
You open a dataset with partitioning="hive". The year partition column comes back as int32, but you wrote it as int64. Why, and how do you get int64?
Show answer
With the string "hive", PyArrow infers partition types from the directory names, and integer-looking values become int32. Pass an explicit schema instead: ds.partitioning(pa.schema([("year", pa.int64())]), flavor="hive").