Sub topic 13
Partition sizing and repartitioning
This chapter assumes you can build a Dask DataFrame, know that operations are lazy until you call compute(), and have read Parquet files with dd.read_parquet. Here you will learn how partition size affects speed and memory, and how to change it.
Why partition size matters
Every partition is a task unit. The scheduler adds a small overhead to each task, usually a fraction of a millisecond up to a few milliseconds. Each partition must also fit in a worker's memory, along with temporary copies made during an operation.
- Too many tiny partitions: the overhead dominates, the task graph grows large, and the scheduler itself becomes the bottleneck.
- Too few huge partitions: you lose parallelism, and a single partition may not fit in memory next to its working copies.
A common rule of thumb is to aim for partitions of about 100 MB in memory. Many workloads do well anywhere between 100 MB and 1 GB. Keep the number of partitions at least as large as the number of worker threads, or some cores sit idle.
Inspecting partitions
Check the current layout before changing it. npartitions gives the count, and memory_usage shows how much memory each partition uses.
import pandas as pd
import dask.dataframe as dd
pdf = pd.DataFrame({
"zone": ["a", "b"] * 50_000,
"fare": range(100_000),
})
ddf = dd.from_pandas(pdf, npartitions=10)
print(ddf.npartitions)
sizes = ddf.map_partitions(lambda part: part.memory_usage(deep=True).sum()).compute()
print(sizes)The result is a pandas Series with one number of bytes per partition. Uneven values point to skewed partitions, often caused by filtering.
Repartitioning
repartition changes the layout. It has two common forms.
ddf = ddf.repartition(partition_size="100MB")
print(ddf.npartitions)partition_size regroups data so each partition is roughly the given size in memory. You can also set the count directly with npartitions.
smaller = ddf.repartition(npartitions=2)
print(smaller.npartitions)Reducing the count mostly merges neighboring partitions and is cheap. Increasing the count splits partitions. Neither form sorts or groups rows by value, so it does not replace a shuffle or set_index.
When to repartition
- After a selective filter. Keeping 1% of rows leaves the same number of partitions, now very small. Repartition before further work.
- After reading many small files. One file per partition can produce thousands of tiny partitions.
- Before writing Parquet. Each partition becomes its own file, so the partition count sets the file count and file size.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Never repartitioning after a heavy filter | Thousands of near-empty tasks | Repartition after filtering |
| Setting `npartitions` to 1 "to be safe" | No parallelism, memory spikes | Use about 100 MB partitions |
| Repartitioning inside every step | Repeated overhead | Repartition once, then `persist()` |
Exercise
You filter a 50 GB dataset with 500 partitions down to about 200 MB of matching rows. What problem remains, and how do you fix it?
Show answer
The 200 MB is still spread over 500 partitions, about 0.4 MB each, so scheduler overhead dominates. Call repartition (for example partition_size="100MB", which gives about 2 partitions, or a small npartitions that matches your worker threads) after the filter, then persist if you reuse the result.
Exercise
Does repartition(npartitions=4) group rows with the same zone value into the same partition?
Show answer
No. It only merges or splits partitions by position. To group rows by key you need a shuffle or set_index.