Sub topic 1
Refresher: Arrow Tables and Schemas
Prerequisites
You should know basic Python, columns and tables, local file paths, and the general purpose of Parquet files.
Arrow tables and schemas
A pyarrow.Table is a column-oriented table. Each column has a name, a data type, and a sequence of values. An Arrow schema records the column names and types separately from the data.
import pyarrow as pa
table = pa.table({
"year": [2023, 2024],
"country": ["DE", "FR"],
"amount": [10.5, 20.0],
})
print(table.schema)
print(table.to_pydict())A schema makes data types explicit. This helps PyArrow validate values and combine data from multiple files.
Parquet essentials
Parquet is a column-oriented file format. A file is divided into row groups. Each row group contains a subset of rows. Within a row group, each column is stored in a column chunk.
This layout lets a reader load only requested columns. Statistics for row groups can also help skip groups that cannot match a filter.
From files to a dataset
The pyarrow.dataset API treats several files as one logical table. A dataset contains file fragments. A fragment usually represents one file, together with metadata such as its schema and partition expression.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
print(dataset.schema)
print(dataset.files)A dataset can discover files, combine their schemas, and plan filtered reads without loading every value immediately.
Partition values in directory names
Partitioning stores selected column values in the directory path. For example:
sales/year=2024/country=DE/part-0.parquetThis path says that every row in the file has year = 2024 and country = "DE". A filter on those columns can allow PyArrow to skip unrelated directories.
Hive versus directory partitioning
| Flavor | Example path | How names are found |
|---|---|---|
| Hive | `year=2024/part.parquet` | Name and value are both in the path |
| Directory | `2024/part.parquet` | The reader needs a declared field order |
Hive partitioning is usually easier to inspect and discover automatically.
Declaring partition schemas and types
Partition values are parsed using a schema. Do not assume every path value should be a string. Declare types when using directory partitioning or when consistent typing matters.
import pyarrow as pa
import pyarrow.dataset as ds
partition_schema = pa.schema([("year", pa.int16())])
dataset = ds.dataset(
"sales_dir",
format="parquet",
partitioning=ds.partitioning(partition_schema),
)
print(dataset.schema)Exercise
What does year=2024 contribute when PyArrow opens a Hive-partitioned file?
Show answer
It contributes a partition expression stating that every row in that fragment has year equal to 2024.