| 1 |
Working with Collections |
| 2 |
======================== |
| 3 |
|
| 4 |
Often we want to do a bit of custom work with ``dask.delayed`` (for example, |
| 5 |
for complex data ingest), then leverage the algorithms in ``dask.array`` or |
| 6 |
``dask.dataframe``, and then switch back to custom work. To this end, all |
| 7 |
collections support ``from_delayed`` functions and ``to_delayed`` |
| 8 |
methods. |
| 9 |
|
| 10 |
As an example, consider the case where we store tabular data in a custom format |
| 11 |
not known by Dask DataFrame. This format is naturally broken apart into |
| 12 |
pieces and we have a function that reads one piece into a Pandas DataFrame. |
| 13 |
We use ``dask.delayed`` to lazily read these files into Pandas DataFrames, |
| 14 |
use ``dd.from_delayed`` to wrap these pieces up into a single |
| 15 |
Dask DataFrame, use the complex algorithms within the DataFrame |
| 16 |
(groupby, join, etc.), and then switch back to ``dask.delayed`` to save our results |
| 17 |
back to the custom format: |
| 18 |
|
| 19 |
.. code-block:: python |
| 20 |
|
| 21 |
import dask.dataframe as dd |
| 22 |
from dask.delayed import delayed |
| 23 |
|
| 24 |
from my_custom_library import load, save |
| 25 |
|
| 26 |
filenames = ... |
| 27 |
dfs = [delayed(load)(fn) for fn in filenames] |
| 28 |
|
| 29 |
df = dd.from_delayed(dfs) |
| 30 |
df = ... # do work with dask.dataframe |
| 31 |
|
| 32 |
dfs = df.to_delayed() |
| 33 |
writes = [delayed(save)(df, fn) for df, fn in zip(dfs, filenames)] |
| 34 |
|
| 35 |
dd.compute(*writes) |
| 36 |
|
| 37 |
Data science is often complex, and ``dask.delayed`` provides a release valve for |
| 38 |
users to manage this complexity on their own, and solve the last mile problem |
| 39 |
for custom formats and complex situations. |