Sub topic 11
Metadata (meta) and how Dask infers output types
This chapter assumes you have read the earlier chapters on lazy evaluation and map_partitions. It also assumes you know pandas dtypes.
What meta is
A Dask DataFrame is lazy, so it must know the column names and dtypes of every result before any data is loaded. That description is called meta. It is an empty pandas object that has the right columns, dtypes and index type but no rows.
Meta lets Dask do three things without computing anything:
- Print the structure of a result.
- Check that
ddf["fare_cents"]refers to a real column. - Build the next step of the task graph.
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.dtypes) # comes from meta, no data is readHow Dask infers meta
For built-in operations such as filters, assign and groupby().mean(), Dask knows the output types and computes meta itself.
For your own function, as in map_partitions, Dask cannot see inside it. It calls the function on a small fake frame with the right columns and dtypes, and it uses the result as meta. The fake values are placeholders like 1 or "foo", not your real data.
Passing meta yourself
Inference is a guess, and a function can run on real data but fail on fake data. Examples are code that parses dates or code that depends on actual values. Passing meta skips the guess and is often faster.
def add_fare_cents(part):
return part.assign(fare_cents=(part["fare"] * 100).astype("int64"))
meta = ddf.head(0).assign(fare_cents=pd.Series(dtype="int64"))
ddf2 = ddf.map_partitions(add_fare_cents, meta=meta)
print(ddf2.dtypes)
print(ddf2.compute())Here ddf.head(0) returns an empty frame with the existing columns, and assign adds the new column. The same approach works for other output shapes:
| Function returns | Meta to pass |
|---|---|
| A DataFrame | An empty pandas DataFrame with the output columns |
| A Series | A tuple such as `("fare_cents", "int64")` |
| A scalar per partition | A value of the right type, such as `0.0` |
Common errors
Meta that does not match the real output. Dask does not check meta against the computed partitions when it builds the graph. If you declare int64 but the function returns float64, the graph is built without complaint. The failure shows up later, in a downstream step, or as wrong dtypes in the final result. If a result has surprising dtypes, compare ddf2.dtypes with ddf2.compute().dtypes.
Inference failing on fake data. The error usually mentions that Dask could not determine meta. The fix is to pass meta= explicitly.
Meta changing with the data. If the columns depend on the values, as with a pivot, Dask cannot know them in advance. Compute the column list first, or use a different approach.
Object columns. Columns of mixed Python objects have dtype object, and Dask can only guess what they hold. Prefer explicit dtypes such as string or category.
Exercise
A function passed to mappartitions parses a "date" column with pd.todatetime, and inference fails because the fake frame contains invalid dates. What should you do, and what does meta need to say about that column?
Show answer
Pass meta explicitly so Dask skips running the function on the fake frame. Build an empty pandas DataFrame with the output columns, and give the date column the dtype "datetime64[ns]".