Sub topic 11
Parquet compression and partitioned datasets
This chapter assumes you can already write a DataFrame with to_parquet and that pyarrow is installed. It covers how to pick a compression codec and how to split one dataset into a directory of files by column value.
Choosing a compression codec
Parquet compresses each column chunk inside the file. This is different from CSV, where you compress the whole file. You choose the codec with the compression argument.
The default is "snappy". It is fast and gives moderate size reduction. Other common values are listed below.
| Codec | Speed | File size | Typical use |
|---|---|---|---|
| `"snappy"` | Fast | Medium | Default, general work |
| `"gzip"` | Slower | Smaller | Widely supported, archives |
| `"zstd"` | Fast to medium | Small | Good balance, often a better default |
| `"brotli"` | Slow to write | Small | Rarely needed |
| `None` | Fastest | Largest | Debugging, already-compressed data |
Which codecs work depends on how your engine was built. pyarrow supports all of the above in its standard wheels. If a codec is missing, the write raises an error that names it.
import pandas as pd
df = pd.DataFrame({
"country": ["DE", "DE", "FR"],
"amount": [10.0, 12.5, 8.0],
})
df.to_parquet("sales_zstd.parquet", compression="zstd", index=False)Readers detect the codec from the file metadata. You do not pass it to read_parquet.
Why partition a dataset
A partitioned dataset is a directory of Parquet files. Pandas creates one subdirectory for each distinct value of the columns you name in partition_cols. Tools such as Spark, DuckDB, and pyarrow can then skip whole directories when a query filters on those columns.
sales = pd.DataFrame({
"country": ["DE", "DE", "FR"],
"amount": [10.0, 12.5, 8.0],
})
sales.to_parquet(
"sales_parquet",
engine="pyarrow",
compression="zstd",
partition_cols=["country"],
)The path is now a directory, not a file. The layout looks like this:
sales_parquet/
country=DE/
<generated-name>.parquet
country=FR/
<generated-name>.parquetThe partition column is stored in the folder name, not inside the files. When you read the directory back, pandas rebuilds the column from the folder names.
back = pd.read_parquet("sales_parquet")
print(back.dtypes)The rebuilt country column comes back as a categorical dtype, not as the original string dtype. Convert it if you need plain strings:
back["country"] = back["country"].astype("string")You can also read only some partitions by passing filters, which the pyarrow engine supports:
de_only = pd.read_parquet(
"sales_parquet",
filters=[("country", "==", "DE")],
)Choosing partition columns
Partitioning helps only when the choice fits how you query. These rules prevent most problems:
- Pick columns with a small number of distinct values, such as country, year, or month.
- Avoid columns with thousands of distinct values, such as
customer_id. Each value creates a folder and a file, and many tiny files read slowly. - Choose columns that your queries filter on often.
- Aim for files of tens to hundreds of megabytes, not a few kilobytes.
Common errors
- Writing to a path that already holds data. Running the partitioned write twice adds new files next to the old ones, because each file name is generated. A repeated run can leave duplicate rows. Delete the old directory first, or write to a fresh path.
- Expecting a single file. Code that opens
sales_parquetas one file fails. Pass the directory toread_parquetinstead. - Passing
index=Falseand then losing partition logic.index=Falseis fine with partitioning. It only controls whether the index is stored. - Missing values in a partition column. Rows with a missing value go into a folder named
country=__HIVE_DEFAULT_PARTITION__. Check for this before you write.
Exercise
You write a DataFrame with partition_cols=["customer_id"], and the column has 200,000 distinct values. What goes wrong, and what would you do instead?
Show answer
The write creates about 200,000 folders, each with a tiny file. Writing and reading both become slow, and listing the directory is expensive. Partition by a low-cardinality column such as year or country that matches your common filters, and keep customer_id as an ordinary column.