Sub topic 14
Common pitfalls and how to debug them
This chapter assumes you know pandas, and that you have read the earlier chapters on partitions, lazy evaluation, compute and persist, schedulers, and partition sizing. It covers the mistakes that cost the most time and how to find them.
Pitfall 1: Calling compute too early or too often
compute() returns a pandas object in your local process. If the result is large, you lose the benefit of Dask and may run out of memory. If you call it many times on related data, Dask repeats the shared work each time.
Keep results lazy as long as you can. Compute only small outputs, such as aggregations. If several results share the same input, persist the input once, or compute the results together with dask.compute.
import dask
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)
total = ddf["fare"].sum()
count = ddf["fare"].count()
# One pass over the data instead of two
total_value, count_value = dask.compute(total, count)
print(total_value / count_value)Pitfall 2: Treating a Dask DataFrame like pandas
Some pandas habits do not carry over.
- Row positions are per partition.
ddf.iloc[5]is not supported on rows, because Dask does not know how many rows each partition holds without computing. - The index may repeat across partitions. After
from_pandasit is unique, but after reading files each partition often starts at 0. - Operations that need all the data in order, or all rows with the same key together, cause a shuffle. A shuffle moves data between partitions and is slow.
- Mutating in place does not work. Assign the result:
ddf = ddf.assign(...).
Pitfall 3: Wrong or missing meta
When Dask cannot infer the output of a function, it runs the function on a small fake frame and guesses. If your function fails on fake data, or returns different columns on real data, you get errors that appear only at compute time. Pass meta explicitly to map_partitions when the output is not obvious.
meta = {"zone": "object", "fare": "float64", "fare_cents": "int64"}
def add_fare_cents(part):
return part.assign(fare_cents=(part["fare"] * 100).astype("int64"))
ddf2 = ddf.map_partitions(add_fare_cents, meta=meta)
print(ddf2.compute())Pitfall 4: Partitions that are too small or too large
Thousands of tiny partitions make the scheduler spend more time on bookkeeping than on work. A few huge partitions can exceed worker memory. Aim for partitions of about 100 MB in memory, and use repartition(partition_size="100MB") after filtering heavily.
Debugging workflow
- Run on a small sample. Use
ddf.head()orddf.partitions[0].compute()to test logic on one partition. - Switch to the synchronous scheduler. It runs everything in one thread, so tracebacks are plain and
pdbworks. - Check the graph and partition count before computing:
ddf.npartitionsandddf.visualize(). - Use the dashboard from a
Clientto see which tasks are slow and whether workers are running out of memory.
mean_fare = ddf.groupby("zone")["fare"].mean()
print(mean_fare.compute(scheduler="synchronous"))Summary table
| Symptom | Likely cause | Fix |
|---|---|---|
| Out of memory on `compute()` | Result too large | Aggregate first, or write to Parquet |
| Same work repeated | Several separate `compute()` calls | `dask.compute` or `persist` |
| Error only at compute time | Wrong meta | Pass `meta` explicitly |
| Slow, many small tasks | Tiny partitions | `repartition` |
| Slow joins or sorts | Shuffle | Set the index once, then persist |
Exercise
A pipeline calls .compute() on three results that all start from the same filtered DataFrame, and the filter is expensive. Name two ways to avoid repeating the filter work.
Show answer
Persist the filtered DataFrame so its partitions stay in memory, then compute the three results from it. Alternatively, pass all three lazy results to a single dask.compute(...) call, so Dask computes the shared filter once.
Exercise
Your map_partitions call works on a tiny sample but fails at compute time with a column mismatch. Which argument should you supply, and which scheduler helps you see the real traceback?
Show answer
Supply meta to describe the output columns and dtypes. Use scheduler="synchronous" to run in one thread and get a readable traceback.