Sub topic 7
Reading and writing Parquet
This chapter assumes you can build a Dask DataFrame, know that operations are lazy, and have used compute(). You should also know basic pandas selection and the idea of partitions.
Why Parquet suits Dask
Parquet is a columnar file format. It stores each column's values together, with the data type recorded in the file and compression applied. This has three effects that matter in Dask:
- Dask can read only the columns you ask for, so unused columns never leave the disk.
- Types are stored in the file, so Dask does not guess them from text the way it does for CSV.
- A dataset can be a directory of many files, which maps well onto partitions.
CSV has none of these properties. For repeated analysis, converting CSV to Parquet once is usually worth the effort.
Writing a Parquet dataset
to_parquet writes one file per partition into a directory. It runs the task graph, so it is an action like compute().
import pandas as pd
import dask.dataframe as dd
pdf = pd.DataFrame({
"zone": ["a", "b", "a", "b"],
"fare": [10.0, 20.0, 30.0, 40.0],
})
ddf = dd.from_pandas(pdf, npartitions=2)
ddf.to_parquet("out/fares.parquet")The path out/fares.parquet is a directory, not a single file. Inside it you will find files such as part.0.parquet and part.1.parquet, one for each partition. Dask also writes the index when it is meaningful.
You need a Parquet engine installed, normally pyarrow.
Reading with column selection
subset = dd.read_parquet("out/fares.parquet", columns=["zone", "fare"])
print(subset.npartitions)read_parquet is lazy. It reads the file metadata to learn the schema and the file layout, and it loads no data until you call compute(). Passing columns pushes the selection down to the reader, so only those columns are read from disk.
You can get the same effect by selecting columns after reading, for example ddf[["zone", "fare"]]. Dask can often optimize this, but passing columns states your intent clearly and works reliably.
How the pieces connect
Practical points
- Partitions and files. The number of files written equals the number of partitions. Many tiny partitions produce many tiny files, which slows reading. Repartition before writing if needed (see the partition sizing chapter).
- Compression. The default is usually snappy, which is fast and compact. You can set another codec with
compression=. - Filters.
read_parquet(..., filters=[("fare", ">", 15)])lets the reader skip row groups whose statistics rule out a match. It is a coarse filter that can leave extra rows, so keep your normalddf[ddf["fare"] > 15]step. - Overwriting. Writing to an existing directory can leave stale files from an earlier run. Use
overwrite=Trueonly when you are sure the directory holds nothing you need.
Common errors
| Symptom | Likely cause |
|---|---|
| `ImportError` about an engine | `pyarrow` is not installed |
| Very slow reads from many small files | Too many small partitions when writing |
| Column not found with `columns=` | Misspelled name, or the column is stored as the index |
| Different dtypes across files | Files were written with different schemas |
Exercise
You wrote a Dask DataFrame with 200 partitions to Parquet, and later reads are slow. Why might this happen, and what can you change before writing?
Show answer
The write produces 200 files, and if the data is small each file is tiny, so the per-file overhead dominates. Repartition to fewer, larger partitions (for example with repartition(partition_size="100MB")) before calling to_parquet.
Exercise
Why does dd.read_parquet(path, columns=["fare"]) use less memory than reading the whole dataset and then selecting fare?
Show answer
Parquet stores columns separately, so the reader loads only the requested column from disk. Reading everything first loads all columns before the selection.