| 1 |
Dask DataFrame |
| 2 |
============== |
| 3 |
|
| 4 |
.. meta:: |
| 5 |
:description: A Dask DataFrame is a large parallel DataFrame composed of many smaller pandas DataFrames, split along the index. These pandas DataFrames may live on disk for larger-than-memory computing on a single machine, or on many different machines in a cluster. |
| 6 |
|
| 7 |
.. toctree:: |
| 8 |
:maxdepth: 1 |
| 9 |
:hidden: |
| 10 |
|
| 11 |
Load and Save Data <dataframe-create.rst> |
| 12 |
Internal Design <dataframe-design.rst> |
| 13 |
Optimizer <dataframe-optimizer.rst> |
| 14 |
Best Practices <dataframe-best-practices.rst> |
| 15 |
API <dataframe-api.rst> |
| 16 |
dataframe-extra.rst |
| 17 |
|
| 18 |
.. grid:: 1 1 2 2 |
| 19 |
|
| 20 |
.. grid-item:: |
| 21 |
:columns: 12 12 8 8 |
| 22 |
|
| 23 |
Dask DataFrame helps you process large tabular data by parallelizing pandas, |
| 24 |
either on your laptop for larger-than-memory computing, or on a distributed |
| 25 |
cluster of computers. |
| 26 |
|
| 27 |
- **Just pandas:** Dask DataFrames are a collection of many pandas DataFrames. |
| 28 |
|
| 29 |
The API is the same. The execution is the same. |
| 30 |
- **Large scale:** Works on 100 GiB on a laptop, or 100 TiB on a cluster. |
| 31 |
- **Easy to use:** Pure Python, easy to set up and debug. |
| 32 |
|
| 33 |
.. grid-item:: |
| 34 |
:columns: 12 12 4 4 |
| 35 |
|
| 36 |
.. image:: images/dask-dataframe.svg |
| 37 |
:alt: Column of four squares collectively labeled as a Dask DataFrame with a single constituent square labeled as a pandas DataFrame. |
| 38 |
|
| 39 |
Dask DataFrames coordinate many pandas DataFrames/Series arranged along the |
| 40 |
index. A Dask DataFrame is partitioned *row-wise*, grouping rows by index value |
| 41 |
for efficiency. These pandas objects may live on disk or on other machines. |
| 42 |
|
| 43 |
From pandas to Dask |
| 44 |
------------------- |
| 45 |
|
| 46 |
Dask DataFrame copies pandas, and so should be familiar to most users |
| 47 |
|
| 48 |
.. tab-set:: |
| 49 |
|
| 50 |
.. tab-item:: Load Data |
| 51 |
|
| 52 |
Pandas and Dask have the same API, and so switching from one to the other |
| 53 |
is straightforward. |
| 54 |
|
| 55 |
.. grid:: 1 1 2 2 |
| 56 |
|
| 57 |
.. grid-item:: |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
>>> import pandas as pd |
| 62 |
|
| 63 |
>>> df = pd.read_parquet('s3://mybucket/myfile.parquet') |
| 64 |
>>> df.head() |
| 65 |
0 1 a |
| 66 |
1 2 b |
| 67 |
2 3 c |
| 68 |
|
| 69 |
.. grid-item:: |
| 70 |
|
| 71 |
.. code-block:: python |
| 72 |
|
| 73 |
>>> import dask.dataframe as dd |
| 74 |
|
| 75 |
>>> df = dd.read_parquet('s3://mybucket/myfile.*.parquet') |
| 76 |
>>> df.head() |
| 77 |
0 1 a |
| 78 |
1 2 b |
| 79 |
2 3 c |
| 80 |
|
| 81 |
.. tab-item:: Data Processing |
| 82 |
|
| 83 |
Dask does pandas in parallel. |
| 84 |
Dask is lazy; when you want an in-memory result add ``.compute()``. |
| 85 |
|
| 86 |
.. grid:: 1 1 2 2 |
| 87 |
|
| 88 |
.. grid-item:: |
| 89 |
|
| 90 |
.. code-block:: python |
| 91 |
|
| 92 |
>>> import pandas as pd |
| 93 |
|
| 94 |
>>> df = df[df.value >= 0] |
| 95 |
>>> joined = df.merge(other, on="account") |
| 96 |
>>> result = joined.groupby("account").value.mean() |
| 97 |
|
| 98 |
>>> result |
| 99 |
alice 123 |
| 100 |
bob 456 |
| 101 |
|
| 102 |
|
| 103 |
.. grid-item:: |
| 104 |
|
| 105 |
.. code-block:: python |
| 106 |
|
| 107 |
>>> import dask.dataframe as dd |
| 108 |
|
| 109 |
>>> df = df[df.value >= 0] |
| 110 |
>>> joined = df.merge(other, on="account") |
| 111 |
>>> result = joined.groupby("account").value.mean() |
| 112 |
|
| 113 |
>>> result.compute() |
| 114 |
alice 123 |
| 115 |
bob 456 |
| 116 |
|
| 117 |
.. tab-item:: Machine Learning |
| 118 |
|
| 119 |
Machine learning libraries often have Dask submodules that |
| 120 |
expect Dask DataFrames and operate in parallel. |
| 121 |
|
| 122 |
.. grid:: 1 1 2 2 |
| 123 |
|
| 124 |
.. grid-item:: |
| 125 |
|
| 126 |
.. code-block:: python |
| 127 |
|
| 128 |
>>> import pandas as pd |
| 129 |
>>> import xgboost |
| 130 |
>>> from sklearn.cross_validation import train_test_split |
| 131 |
|
| 132 |
>>> X_train, X_test, y_train, y_test = train_test_split( |
| 133 |
... X, y, test_size=0.2, |
| 134 |
) |
| 135 |
>>> dtrain = xgboost.DMatrix(X_train, label=y_train) |
| 136 |
|
| 137 |
>>> xgboost.train(params, dtrain, 100) |
| 138 |
<xgboost.Booster ...> |
| 139 |
|
| 140 |
.. grid-item:: |
| 141 |
|
| 142 |
.. code-block:: python |
| 143 |
|
| 144 |
>>> import dask.dataframe as dd |
| 145 |
>>> import xgboost.dask |
| 146 |
>>> from dask_ml.model_selection import train_test_split |
| 147 |
|
| 148 |
>>> X_train, X_test, y_train, y_test = train_test_split( |
| 149 |
... X, y, test_size=0.2, |
| 150 |
) |
| 151 |
>>> dtrain = xgboost.dask.DaskDMatrix(client, X, y) |
| 152 |
|
| 153 |
>>> xgboost.dask.train(params, dtrain, 100) |
| 154 |
<xgboost.Booster ...> |
| 155 |
|
| 156 |
As with all Dask collections, you trigger computation by calling the |
| 157 |
``.compute()`` method or persist data in distributed memory with the |
| 158 |
``.persist()`` method. |
| 159 |
|
| 160 |
When not to use Dask DataFrames |
| 161 |
------------------------------- |
| 162 |
|
| 163 |
Dask DataFrames are often used either when ... |
| 164 |
|
| 165 |
1. Your data is too big |
| 166 |
2. Your computation is too slow and other techniques don't work |
| 167 |
|
| 168 |
You should probably stick to just using pandas if ... |
| 169 |
|
| 170 |
1. Your data is small |
| 171 |
2. Your computation is fast (subsecond) |
| 172 |
3. There are simpler ways to accelerate your computation, like avoiding |
| 173 |
``.apply`` or Python for loops and using a built-in pandas method instead. |
| 174 |
|
| 175 |
Examples |
| 176 |
-------- |
| 177 |
|
| 178 |
Dask DataFrame is used across a wide variety of applications — anywhere where working |
| 179 |
with large tabular dataset. Here are a few large-scale examples: |
| 180 |
|
| 181 |
- `Parquet ETL with Dask DataFrame <https://docs.coiled.io/user_guide/uber-lyft.html?utm_source=dask-docs&utm_medium=dataframe>`_ |
| 182 |
- `XGBoost model training with Dask DataFrame <https://docs.coiled.io/user_guide/xgboost.html?utm_source=dask-docs&utm_medium=dataframe>`_ |
| 183 |
- `Visualize 1,000,000,000 points <https://docs.coiled.io/user_guide/datashader.html?utm_source=dask-docs&utm_medium=dataframe>`_ |
| 184 |
|
| 185 |
These examples all process larger-than-memory datasets on Dask clusters deployed with |
| 186 |
`Coiled <https://coiled.io/?utm_source=dask-docs&utm_medium=dataframe>`_, |
| 187 |
but there are many options for managing and deploying Dask. |
| 188 |
See our :doc:`deploying` documentation for more information on deployment options. |
| 189 |
|
| 190 |
You can also visit https://examples.dask.org/dataframe.html for a collection of additional examples. |
| 191 |
|
| 192 |
.. raw:: html |
| 193 |
|
| 194 |
<iframe width="560" |
| 195 |
height="315" |
| 196 |
src="https://www.youtube.com/embed/AT2XtFehFSQ" |
| 197 |
style="margin: 0 auto 20px auto; display: block;" |
| 198 |
frameborder="0" |
| 199 |
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" |
| 200 |
allowfullscreen></iframe> |