Sub topic 1
When pandas is not enough, and what Dask adds
Prerequisites
This chapter assumes you know basic Python, including functions and imports. It also assumes you know pandas DataFrame fundamentals: selecting, filtering, assign, groupby and aggregation. You should be familiar with reading CSV files and understand that a single machine has a memory limit.
Where pandas runs out
pandas holds a whole DataFrame in memory and runs most operations on one CPU core. That works well until one of two things happens:
- The data does not fit in RAM. A common rule of thumb is that pandas needs several times the file size in memory, because many operations create temporary copies. A 5 GB CSV can exhaust a 16 GB laptop.
- The work is too slow. The data fits, but a long pipeline uses one core while the others sit idle.
The usual symptoms are a MemoryError, a process killed by the operating system, or a notebook cell that runs for many minutes.
What Dask adds
Dask is a Python library for parallel computing. Its DataFrame class, dask.dataframe.DataFrame, looks like a pandas DataFrame but is split into many smaller pandas DataFrames called partitions. Dask adds three things:
- Out-of-core processing. Only some partitions need to be in memory at a time, so a dataset can be larger than RAM.
- Parallelism. Dask runs operations on different partitions at the same time, on several cores or several machines.
- Laziness. Operations are recorded as a plan, called a task graph, and run only when you ask for a result.
The API deliberately mirrors pandas, so most of what you know carries over.
A first look
The following code splits a small pandas DataFrame into two partitions. Printing the result shows its structure without computing any data.
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)
print(ddf.npartitions) # 2
print(ddf) # shows column names and dtypes, not dataThe printout lists column names and dtypes but no rows. Dask has not loaded or computed anything. The next chapters explain why.
When to choose Dask
Dask is not always the better choice. It adds scheduling overhead and some pandas features are missing or behave differently.
| Situation | Better fit |
|---|---|
| Data fits comfortably in memory and runs fast | pandas |
| Data is larger than RAM | Dask |
| Data fits, but the pipeline is slow and uses one core | Dask, or faster pandas code first |
| Many files that can be processed independently | Dask |
| Work that needs row-by-row access or heavy random indexing | pandas |
Before moving to Dask, check whether simpler fixes are enough. You can read only the columns you need, use smaller dtypes such as category or float32, or read the file in chunks. If those fail, Dask is the next step.
Exercises
Exercise
You have a 40 GB CSV and a laptop with 16 GB of RAM. Why does pandas fail here, and which two features of Dask address the problem?
Show answer
pandas loads the entire dataset into memory, and 40 GB cannot fit in 16 GB. Dask splits the data into partitions so only some are in memory at a time (out-of-core processing), and it runs work on partitions in parallel using lazy task graphs that execute only when you request a result.
Exercise
A 200 MB CSV loads into pandas in two seconds and your groupby finishes in under a second. Should you move to Dask? Why or why not?
Show answer
No. The data fits easily in memory and the work is already fast. Dask's scheduling overhead would likely make it slower, and some pandas features may not be supported.