| 1 |
.. _dataframe.indexing: |
| 2 |
|
| 3 |
Indexing into Dask DataFrames |
| 4 |
============================= |
| 5 |
|
| 6 |
Dask DataFrame supports some of Pandas' indexing behavior. |
| 7 |
|
| 8 |
.. currentmodule:: dask.dataframe |
| 9 |
|
| 10 |
.. autosummary:: |
| 11 |
|
| 12 |
DataFrame.iloc |
| 13 |
DataFrame.loc |
| 14 |
|
| 15 |
Label-based Indexing |
| 16 |
-------------------- |
| 17 |
|
| 18 |
Just like Pandas, Dask DataFrame supports label-based indexing with the ``.loc`` |
| 19 |
accessor for selecting rows or columns, and ``__getitem__`` (square brackets) |
| 20 |
for selecting just columns. |
| 21 |
|
| 22 |
.. note:: |
| 23 |
|
| 24 |
To select rows, the DataFrame's divisions must be known (see |
| 25 |
:ref:`dataframe.design` and :ref:`dataframe.performance` for more information.) |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
>>> import dask.dataframe as dd |
| 30 |
>>> import pandas as pd |
| 31 |
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [3, 4, 5]}, |
| 32 |
... index=['a', 'b', 'c']) |
| 33 |
>>> ddf = dd.from_pandas(df, npartitions=2) |
| 34 |
>>> ddf |
| 35 |
Dask DataFrame Structure: |
| 36 |
A B |
| 37 |
npartitions=1 |
| 38 |
a int64 int64 |
| 39 |
c ... ... |
| 40 |
Dask Name: from_pandas, 1 tasks |
| 41 |
|
| 42 |
Selecting columns: |
| 43 |
|
| 44 |
.. code-block:: python |
| 45 |
|
| 46 |
>>> ddf[['B', 'A']] |
| 47 |
Dask DataFrame Structure: |
| 48 |
B A |
| 49 |
npartitions=1 |
| 50 |
a int64 int64 |
| 51 |
c ... ... |
| 52 |
Dask Name: getitem, 2 tasks |
| 53 |
|
| 54 |
|
| 55 |
Selecting a single column reduces to a Dask Series: |
| 56 |
|
| 57 |
.. code-block:: python |
| 58 |
|
| 59 |
>>> ddf['A'] |
| 60 |
Dask Series Structure: |
| 61 |
npartitions=1 |
| 62 |
a int64 |
| 63 |
c ... |
| 64 |
Name: A, dtype: int64 |
| 65 |
Dask Name: getitem, 2 tasks |
| 66 |
|
| 67 |
Slicing rows and (optionally) columns with ``.loc``: |
| 68 |
|
| 69 |
.. code-block:: python |
| 70 |
|
| 71 |
>>> ddf.loc[['b', 'c'], ['A']] |
| 72 |
Dask DataFrame Structure: |
| 73 |
A |
| 74 |
npartitions=1 |
| 75 |
b int64 |
| 76 |
c ... |
| 77 |
Dask Name: loc, 2 tasks |
| 78 |
|
| 79 |
>>> ddf.loc[df["A"] > 1, ["B"]] |
| 80 |
Dask DataFrame Structure: |
| 81 |
B |
| 82 |
npartitions=1 |
| 83 |
a int64 |
| 84 |
c ... |
| 85 |
Dask Name: try_loc, 2 tasks |
| 86 |
|
| 87 |
>>> ddf.loc[lambda df: df["A"] > 1, ["B"]] |
| 88 |
Dask DataFrame Structure: |
| 89 |
B |
| 90 |
npartitions=1 |
| 91 |
a int64 |
| 92 |
c ... |
| 93 |
Dask Name: try_loc, 2 tasks |
| 94 |
|
| 95 |
Dask DataFrame supports Pandas' `partial-string indexing <https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#partial-string-indexing>`_: |
| 96 |
|
| 97 |
.. code-block:: python |
| 98 |
|
| 99 |
>>> ts = dd.demo.make_timeseries() |
| 100 |
>>> ts |
| 101 |
Dask DataFrame Structure: |
| 102 |
id name x y |
| 103 |
npartitions=11 |
| 104 |
2000-01-31 int64 object float64 float64 |
| 105 |
2000-02-29 ... ... ... ... |
| 106 |
... ... ... ... ... |
| 107 |
2000-11-30 ... ... ... ... |
| 108 |
2000-12-31 ... ... ... ... |
| 109 |
Dask Name: make-timeseries, 11 tasks |
| 110 |
|
| 111 |
>>> ts.loc['2000-02-12'] |
| 112 |
Dask DataFrame Structure: |
| 113 |
id name x y |
| 114 |
npartitions=1 |
| 115 |
2000-02-12 00:00:00.000000000 int64 object float64 float64 |
| 116 |
2000-02-12 23:59:59.999999999 ... ... ... ... |
| 117 |
Dask Name: loc, 12 tasks |
| 118 |
|
| 119 |
|
| 120 |
Positional Indexing |
| 121 |
------------------- |
| 122 |
|
| 123 |
Dask DataFrame does not track the length of partitions, making positional |
| 124 |
indexing with ``.iloc`` inefficient for selecting rows. :meth:`DataFrame.iloc` |
| 125 |
only supports indexers where the row indexer is ``slice(None)`` (which ``:`` is |
| 126 |
a shorthand for.) |
| 127 |
|
| 128 |
.. code-block:: python |
| 129 |
|
| 130 |
>>> ddf.iloc[:, [1, 0]] |
| 131 |
Dask DataFrame Structure: |
| 132 |
B A |
| 133 |
npartitions=1 |
| 134 |
a int64 int64 |
| 135 |
c ... ... |
| 136 |
Dask Name: iloc, 2 tasks |
| 137 |
|
| 138 |
Trying to select specific rows with ``iloc`` will raise an exception: |
| 139 |
|
| 140 |
.. code-block:: python |
| 141 |
|
| 142 |
>>> ddf.iloc[[0, 2], [1]] |
| 143 |
Traceback (most recent call last) |
| 144 |
File "<stdin>", line 1, in <module> |
| 145 |
ValueError: 'DataFrame.iloc' does not support slicing rows. The indexer must be a 2-tuple whose first item is 'slice(None)'. |
| 146 |
|
| 147 |
|
| 148 |
Partition Indexing |
| 149 |
------------------ |
| 150 |
|
| 151 |
In addition to pandas-style indexing, Dask DataFrame also supports indexing at a |
| 152 |
partition level with :meth:`DataFrame.get_partition` and |
| 153 |
:attr:`DataFrame.partitions`. These can be used to select subsets of the data by |
| 154 |
partition, rather than by position in the entire DataFrame or index label. |
| 155 |
|
| 156 |
Use :meth:`DataFrame.get_partition` to select a single partition by position. |
| 157 |
|
| 158 |
.. code-block:: python |
| 159 |
|
| 160 |
>>> import dask |
| 161 |
>>> ddf = dask.datasets.timeseries(start="2021-01-01", end="2021-01-07", freq="1h") |
| 162 |
>>> ddf.get_partition(0) |
| 163 |
Dask DataFrame Structure: |
| 164 |
name id x y |
| 165 |
npartitions=1 |
| 166 |
2021-01-01 object int64 float64 float64 |
| 167 |
2021-01-02 ... ... ... ... |
| 168 |
Dask Name: get-partition, 2 graph layers |
| 169 |
|
| 170 |
Note that the result is also a Dask DatFrame. |
| 171 |
|
| 172 |
Index into :attr:`DataFrame.partitions` to select one or more partitions. For |
| 173 |
example, you can select every other partition with a slice: |
| 174 |
|
| 175 |
|
| 176 |
.. code-block:: python |
| 177 |
|
| 178 |
>>> ddf.partitions[::2] |
| 179 |
Dask DataFrame Structure: |
| 180 |
name id x y |
| 181 |
npartitions=3 |
| 182 |
2021-01-01 object int64 float64 float64 |
| 183 |
2021-01-03 ... ... ... ... |
| 184 |
2021-01-05 ... ... ... ... |
| 185 |
2021-01-06 ... ... ... ... |
| 186 |
Dask Name: blocks, 2 graph layers |
| 187 |
|
| 188 |
Or even more complicated selections based on the data in the partitions |
| 189 |
themselves (at the cost of computing the DataFrame up until that point). For |
| 190 |
example, we can create a boolean mask with the partitions that have more than |
| 191 |
some number of unique IDs using :meth:`DataFrame.map_partitions`: |
| 192 |
|
| 193 |
.. code-block:: python |
| 194 |
|
| 195 |
>>> mask = ddf.id.map_partitions(lambda p: len(p.unique()) > 20).compute() |
| 196 |
>>> ddf.partitions[mask] |
| 197 |
Dask DataFrame Structure: |
| 198 |
name id x y |
| 199 |
npartitions=5 |
| 200 |
2021-01-01 object int64 float64 float64 |
| 201 |
2021-01-02 ... ... ... ... |
| 202 |
... ... ... ... ... |
| 203 |
2021-01-06 ... ... ... ... |
| 204 |
2021-01-07 ... ... ... ... |
| 205 |
Dask Name: blocks, 2 graph layers |