Sub topic 3
Creating Dask DataFrames from files and from pandas
This chapter shows how to build a Dask DataFrame from a pandas DataFrame and from files, and how to inspect the result. It assumes you know basic Python and pandas (selecting, filtering, groupby). It also assumes you know that a single machine has a memory limit. Partitions were introduced in the previous chapter. Here you use them.
Two ways to start
Most Dask work starts from one of two sources:
- An existing pandas DataFrame in memory. This is useful for learning, testing, and small lookup tables.
- Files on disk, such as CSV or Parquet. This is the normal case for data that is too big for pandas.
In both cases the result is a Dask DataFrame, which is a collection of pandas DataFrames (partitions) with a shared set of columns and dtypes.
From a pandas DataFrame
dd.from_pandas splits an in-memory DataFrame into partitions. You choose the count with npartitions.
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 a Dask DataFrame shows its structure: column names, dtypes, and the number of partitions. It does not show rows, because no data has been computed yet. The next chapter explains why.
To look at real rows, use ddf.head(). It computes only the first partition, so it is cheap.
From files
For files, use a reader that matches the format.
import dask.dataframe as dd
# One CSV file, or many files matched by a glob pattern
ddf = dd.read_csv("data/trips-*.csv")
# A Parquet file or a directory of Parquet files
ddf = dd.read_parquet("data/trips.parquet")The glob pattern trips-*.csv matches many files, and Dask combines them into one DataFrame. A single large CSV is split into blocks of bytes, and each block becomes a partition. With many files, each file becomes at least one partition.
Parquet is covered in its own chapter. For now, remember that it is columnar, so you can pass columns=[...] to read only the columns you need.
Inspecting what you built
These attributes read metadata only. They do not load the data.
| Attribute | What it tells you |
|---|---|
| `ddf.npartitions` | Number of partitions |
| `ddf.columns` | Column names |
| `ddf.dtypes` | Column dtypes |
| `ddf.divisions` | Index boundaries between partitions, or `None` values if unknown |
You can also fetch one partition as a pandas DataFrame:
first = ddf.partitions[0].compute()
print(type(first)) # a pandas DataFrameddf.partitions[0] selects the partition lazily, and compute() turns it into a pandas object.
Common errors
- Too few or too many partitions.
from_pandaswithnpartitions=1gives no parallelism. Thousands of tiny partitions add scheduling overhead. A later chapter covers sizing. - Using
from_pandason data that does not fit in memory. The pandas DataFrame must already exist in memory, so this does not solve a memory problem. Read from files instead. - Expecting
len(ddf)to be free. It reads the data to count rows. Use it deliberately.
Exercise
You call dd.read_csv("logs/day-*.csv") and it matches 30 files. You then print the result and see column names and dtypes but no rows. Did the read fail? Which call would show you a few real rows?
Show answer
The read did not fail. Dask only builds the DataFrame's structure and does not load the data until you ask for a result. Calling ddf.head() computes the first partition and returns its first rows as a pandas DataFrame.