Sub topic 2
Partitions: a DataFrame made of many pandas DataFrames
This chapter assumes you know basic Python and pandas (selecting, filtering, assign, groupby). It also assumes you know that one machine has limited memory. The chapter explains the structure that every Dask DataFrame is built on, the partition.
What a partition is
A Dask DataFrame is a collection of pandas DataFrames stacked along the rows. Each piece is a partition. All partitions share the same columns and dtypes. Together they act as one logical table.
Dask does not load or process the whole table at once. It works on one partition at a time, and several partitions can run in parallel on different threads, processes, or machines. Because a partition is an ordinary pandas DataFrame, anything you know about pandas applies inside it.
Why partitions matter
Partitions are the unit of work. This has three consequences:
- Memory: only the partitions being processed need to be in memory, so a dataset larger than RAM can still be handled.
- Parallelism: independent partitions can be processed at the same time.
- Overhead: each partition adds scheduling cost, so very many tiny partitions are slow.
Inspecting partitions
Build a small pandas DataFrame and split it into two partitions.
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 dataPrinting ddf shows the structure (columns, dtypes, number of partitions) but no rows. Dask has not computed anything, and the later chapters on lazy evaluation explain why.
To look at one partition, use get_partition. Calling compute() on it returns a normal pandas DataFrame.
first = ddf.get_partition(0).compute()
print(type(first)) # a pandas DataFrame
print(first)You can also see how many rows each partition holds. This runs a small computation, because Dask does not know row counts in advance.
print(ddf.map_partitions(len).compute())Divisions and the index
Each partition covers a range of index values. Dask can record these boundaries as divisions. When divisions are known, Dask can tell which partition holds a given index value and skip the rest. When they are unknown, Dask must check every partition.
print(ddf.known_divisions) # True for from_pandas with a sorted index
print(ddf.divisions) # boundaries between partitionsDivisions come up again in the chapter on shuffles, joins, and the index.
Partitions are not the same as rows
The number of partitions is a tuning choice, not a property of the data. You pick it when you create the DataFrame, or it comes from how the data is stored. For example, reading a folder of files usually gives at least one partition per file.
Common errors
- Expecting rows when printing.
print(ddf)shows structure only. Useddf.head()to peek at the first rows, which reads just the first partition. - Assuming global order. Operations run per partition. Row order across partitions is only meaningful if the index is sorted.
- Using one giant partition.
npartitions=1removes all parallelism and holds the whole table in one pandas DataFrame.
Exercise
You create ddf = dd.from_pandas(pdf, npartitions=4) from a pandas DataFrame with 1,000 rows. What kind of object is each partition, and why would setting npartitions=1000 be a poor choice?
Show answer
Each partition is an ordinary pandas DataFrame, about 250 rows each in this case. Setting npartitions=1000 would give one row per partition. The scheduling overhead for each tiny task would be far larger than the work done, so the computation would run slower.
Exercise
Which call returns the contents of the first partition as a pandas DataFrame?
Show answer
ddf.get_partition(0).compute(). The get_partition call selects the partition lazily, and compute() runs it and returns the pandas result.