| 1 |
.. _dataframe.design: |
| 2 |
|
| 3 |
Dask DataFrame Design |
| 4 |
===================== |
| 5 |
|
| 6 |
Dask DataFrames coordinate many Pandas DataFrames/Series arranged along an |
| 7 |
index. We define a Dask DataFrame object with the following components: |
| 8 |
|
| 9 |
- A Dask graph with a special set of keys designating partitions, such as |
| 10 |
``('x', 0), ('x', 1), ...`` |
| 11 |
- A name to identify which keys in the Dask graph refer to this DataFrame, such |
| 12 |
as ``'x'`` |
| 13 |
- An empty Pandas object containing appropriate metadata (e.g. column names, |
| 14 |
dtypes, etc.) |
| 15 |
- A sequence of partition boundaries along the index called ``divisions`` |
| 16 |
|
| 17 |
Metadata |
| 18 |
-------- |
| 19 |
|
| 20 |
Many DataFrame operations rely on knowing the name and dtype of columns. To |
| 21 |
keep track of this information, all Dask DataFrame objects have a ``_meta`` |
| 22 |
attribute which contains an empty Pandas object with the same dtypes and names. |
| 23 |
For example: |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
>>> df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'y', 'z']}) |
| 28 |
>>> ddf = dd.from_pandas(df, npartitions=2) |
| 29 |
>>> ddf._meta |
| 30 |
Empty DataFrame |
| 31 |
Columns: [a, b] |
| 32 |
Index: [] |
| 33 |
>>> ddf._meta.dtypes |
| 34 |
a int64 |
| 35 |
b object |
| 36 |
dtype: object |
| 37 |
|
| 38 |
Internally, Dask DataFrame does its best to propagate this information |
| 39 |
through all operations, so most of the time a user shouldn't have to worry |
| 40 |
about this. Usually this is done by evaluating the operation on a small sample |
| 41 |
of fake data, which can be found on the ``_meta_nonempty`` attribute: |
| 42 |
|
| 43 |
.. code-block:: python |
| 44 |
|
| 45 |
>>> ddf._meta_nonempty |
| 46 |
a b |
| 47 |
0 1 foo |
| 48 |
1 1 foo |
| 49 |
|
| 50 |
Sometimes this operation may fail in user defined functions (e.g. when using |
| 51 |
``DataFrame.apply``), or may be prohibitively expensive. For these cases, many |
| 52 |
functions support an optional ``meta`` keyword, which allows specifying the |
| 53 |
metadata directly, avoiding the inference step. For convenience, this supports |
| 54 |
several options: |
| 55 |
|
| 56 |
1. A Pandas object with appropriate dtypes and names. If not empty, an empty |
| 57 |
slice will be taken: |
| 58 |
|
| 59 |
.. code-block:: python |
| 60 |
|
| 61 |
>>> ddf.map_partitions(foo, meta=pd.DataFrame({'a': [1], 'b': [2]})) |
| 62 |
|
| 63 |
2. A description of the appropriate names and dtypes. This can take several forms: |
| 64 |
|
| 65 |
* A ``dict`` of ``{name: dtype}`` or an iterable of ``(name, dtype)`` |
| 66 |
specifies a DataFrame. Note that order is important: the order of the |
| 67 |
names in ``meta`` should match the order of the columns |
| 68 |
* A tuple of ``(name, dtype)`` specifies a series |
| 69 |
|
| 70 |
This keyword is available on all functions/methods that take user provided |
| 71 |
callables (e.g. ``DataFrame.map_partitions``, ``DataFrame.apply``, etc...), as |
| 72 |
well as many creation functions (e.g. ``dd.from_delayed``). |
| 73 |
|
| 74 |
|
| 75 |
.. _dataframe-design-partitions: |
| 76 |
|
| 77 |
Partitions |
| 78 |
---------- |
| 79 |
|
| 80 |
Internally, a Dask DataFrame is split into many partitions, where each partition |
| 81 |
is one Pandas DataFrame. These DataFrames are split vertically along the |
| 82 |
index. When our index is sorted and we know the values of the divisions of our |
| 83 |
partitions, then we can be clever and efficient with expensive algorithms (e.g. |
| 84 |
groupby's, joins, etc...). |
| 85 |
|
| 86 |
For example, if we have a time-series index, then our partitions might be |
| 87 |
divided by month: all of January will live in one partition while all of |
| 88 |
February will live in the next. In these cases, operations like ``loc``, |
| 89 |
``groupby``, and ``join/merge`` along the index can be *much* more efficient |
| 90 |
than would otherwise be possible in parallel. You can view the number of |
| 91 |
partitions and divisions of your DataFrame with the following fields: |
| 92 |
|
| 93 |
.. code-block:: python |
| 94 |
|
| 95 |
>>> df.npartitions |
| 96 |
4 |
| 97 |
>>> df.divisions |
| 98 |
['2015-01-01', '2015-02-01', '2015-03-01', '2015-04-01', '2015-04-31'] |
| 99 |
|
| 100 |
The number of partitions and the division values might change during optimization. |
| 101 |
The optimizer will try to create partitions with a sensible size to avoid straining |
| 102 |
the scheduler with many small partitions. |
| 103 |
|
| 104 |
Divisions includes the minimum value of every partition's index and the maximum |
| 105 |
value of the last partition's index. In the example above, if the user searches |
| 106 |
for a specific datetime range, then we know which partitions we need to inspect |
| 107 |
and which we can drop: |
| 108 |
|
| 109 |
.. code-block:: python |
| 110 |
|
| 111 |
>>> df.loc['2015-01-20': '2015-02-10'] # Must inspect first two partitions |
| 112 |
|
| 113 |
Often we do not have such information about our partitions. When reading CSV |
| 114 |
files, for example, we do not know, without extra user input, how the data is |
| 115 |
divided. In this case ``.divisions`` will be all ``None``: |
| 116 |
|
| 117 |
.. code-block:: python |
| 118 |
|
| 119 |
>>> df.divisions |
| 120 |
[None, None, None, None, None] |
| 121 |
|
| 122 |
In these cases, any operation that requires a cleanly partitioned DataFrame with |
| 123 |
known divisions will have to perform a sort. This can generally achieved by |
| 124 |
calling ``df.set_index(...)``. |
| 125 |
|
| 126 |
|
| 127 |
.. _dataframe-design-groupby: |
| 128 |
|
| 129 |
Groupby |
| 130 |
------- |
| 131 |
|
| 132 |
By default, groupby will choose the number of output partitions based on a few |
| 133 |
different factors. It will look at the number of grouping keys to guess the |
| 134 |
cardinality of your data. It will use this information to calculate a factor |
| 135 |
based on the number of input partitions. You can override this behavior by |
| 136 |
specifying the number of output partitions using the `split_out` argument. |
| 137 |
|
| 138 |
.. code-block:: python |
| 139 |
|
| 140 |
result = df.groupby('id').value.mean() |
| 141 |
result.npartitions # returns 1 |
| 142 |
|
| 143 |
result = df.groupby(['id', 'id2']).value.mean() |
| 144 |
result.npartitions # returns 5 |
| 145 |
|
| 146 |
result = df.groupby('id').value.mean(split_out=8) |
| 147 |
result.npartitions # returns 8 |
| 148 |
|
| 149 |
Some groupby aggregation functions have a different `split_out` default value. |
| 150 |
`split_out=True` will keep the number of partitions constant, which is useful |
| 151 |
for operations that either don't reduce the number of rows very much. |
| 152 |
|
| 153 |
.. code-block:: python |
| 154 |
|
| 155 |
result = df.groupby('id').value.nunique() |
| 156 |
result.npartitions # returns same as df.npartitions |