Debian Codegraph @

docs/source/dataframe-extend.rst

raw
1 Extending DataFrames
2 ====================
3
4 Subclass DataFrames
5 -------------------
6
7 There are a few projects that subclass or replicate the functionality of Pandas
8 objects:
9
10 - GeoPandas: for Geospatial analytics
11 - cuDF: for data analysis on GPUs
12 - ...
13
14 These projects may also want to produce parallel variants of themselves with
15 Dask, and may want to reuse some of the code in Dask DataFrame. Subclassing
16 Dask DataFrames is intended for maintainers of these libraries and not for
17 general users.
18
19
20 Implement dask, name, meta, and divisions
21 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22
23 You will need to implement ``._meta``, ``.dask``, ``.divisions``, and
24 ``._name`` as defined in the :doc:`DataFrame design docs <dataframe-design>`.
25
26
27 Extend Dispatched Methods
28 ^^^^^^^^^^^^^^^^^^^^^^^^^
29
30 If you are going to pass around Pandas-like objects that are not normal Pandas
31 objects, then we ask you to extend a few dispatched methods: ``make_meta``,
32 ``get_collection_type``, and ``concat``.
33
34 make_meta
35 """""""""
36
37 This function returns an empty version of one of your non-Dask objects, given a
38 non-empty non-Dask object:
39
40 .. code-block:: python
41
42 from dask.dataframe.dispatch import make_meta_dispatch
43
44 @make_meta_dispatch.register(MyDataFrame)
45 def make_meta_dataframe(df, index=None):
46 return df.head(0)
47
48
49 @make_meta_dispatch.register(MySeries)
50 def make_meta_series(s, index=None):
51 return s.head(0)
52
53
54 @make_meta_dispatch.register(MyIndex)
55 def make_meta_index(ind, index=None):
56 return ind[:0]
57
58 For dispatching any arbitrary ``object`` types to a respective back-end, we
59 recommend registering a dispatch for ``make_meta_obj``:
60
61 .. code-block:: python
62
63 from dask.dataframe.dispatch import make_meta_obj
64
65 @make_meta_obj.register(MyDataFrame)
66 def make_meta_object(x, index=None):
67 if isinstance(x, dict):
68 return MyDataFrame()
69 elif isinstance(x, int):
70 return MySeries
71 .
72 .
73 .
74
75
76 Additionally, you should create a similar function that returns a non-empty
77 version of your non-Dask DataFrame objects filled with a few rows of
78 representative or random data. This is used to guess types when they are not
79 provided. It should expect an empty version of your object with columns,
80 dtypes, index name, and it should return a non-empty version:
81
82 .. code-block:: python
83
84 from dask.dataframe.utils import meta_nonempty
85
86 @meta_nonempty.register(MyDataFrame)
87 def meta_nonempty_dataframe(df):
88 ...
89 return MyDataFrame(..., columns=df.columns,
90 index=MyIndex(..., name=df.index.name)
91
92
93 @meta_nonempty.register(MySeries)
94 def meta_nonempty_series(s):
95 ...
96
97
98 @meta_nonempty.register(MyIndex)
99 def meta_nonempty_index(ind):
100 ...
101
102
103 get_collection_type
104 """""""""""""""""""
105
106 Given a non-Dask DataFrame object, return the Dask equivalent:
107
108 .. code-block:: python
109
110 from dask.dataframe import get_collection_type
111
112 @get_collection_type.register(MyDataFrame)
113 def get_collection_type_dataframe(df):
114 return MyDaskDataFrame
115
116
117 @get_collection_type.register(MySeries)
118 def get_collection_type_series(s):
119 return MyDaskSeries
120
121
122 @get_collection_type.register(MyIndex)
123 def get_collection_type_index(ind):
124 return MyDaskIndex
125
126
127 concat
128 """"""
129
130 Concatenate many of your non-Dask DataFrame objects together. It should expect
131 a list of your objects (homogeneously typed):
132
133 .. code-block:: python
134
135 from dask.dataframe.methods import concat_dispatch
136
137 @concat_dispatch.register((MyDataFrame, MySeries, MyIndex))
138 def concat_pandas(dfs, axis=0, join='outer', uniform=False, filter_warning=True):
139 ...
140
141
142 .. _extensionarrays:
143
144 Extension Arrays
145 ----------------
146
147 Rather than subclassing Pandas DataFrames, you may be interested in extending
148 Pandas with `Extension Arrays
149 <https://pandas.pydata.org/pandas-docs/stable/extending.html>`_.
150
151 All of the first-party extension arrays (those implemented in pandas itself)
152 are supported directly by dask.
153
154 Developers implementing third-party extension arrays (outside of pandas) will
155 need to do register their ``ExtensionDtype`` with Dask so that it works
156 correctly in ``dask.dataframe``.
157
158 For example, we'll register the *test-only* ``DecimalDtype`` from pandas
159 test suite.
160
161 .. code-block:: python
162
163 from decimal import Decimal
164 from dask.dataframe.extensions import make_array_nonempty, make_scalar
165 from pandas.tests.extension.decimal import DecimalArray, DecimalDtype
166
167 @make_array_nonempty.register(DecimalDtype)
168 def _(dtype):
169 return DecimalArray._from_sequence([Decimal('0'), Decimal('NaN')],
170 dtype=dtype)
171
172
173 @make_scalar.register(Decimal)
174 def _(x):
175 return Decimal('1')
176
177
178 Internally, Dask will use this to create a small dummy Series for tracking
179 metadata through operations.
180
181 .. code-block:: python
182
183 >>> make_array_nonempty(DecimalDtype())
184 <DecimalArray>
185 [Decimal('0'), Decimal('NaN')]
186 Length: 2, dtype: decimal
187
188 So you (or your users) can now create and store a dask ``DataFrame`` or
189 ``Series`` with your extension array contained within.
190
191 .. code-block:: python
192
193 >>> from decimal import Decimal
194 >>> import dask.dataframe as dd
195 >>> import pandas as pd
196 >>> from pandas.tests.extension.decimal import DecimalArray
197 >>> s = pd.Series(DecimalArray([Decimal('0.0')] * 10))
198 >>> ds = dd.from_pandas(s, 3)
199 >>> ds
200 Dask Series Structure:
201 npartitions=3
202 0 decimal
203 4 ...
204 8 ...
205 9 ...
206 dtype: decimal
207 Dask Name: from_pandas, 3 tasks
208
209 Notice the ``decimal`` dtype.
210
211 .. _dataframe.accessors:
212
213 Accessors
214 ---------
215
216 Many extension arrays expose their functionality on Series or DataFrame objects
217 using accessors. Dask provides decorators to register accessors similar to pandas. See
218 `the pandas documentation on accessors <http://pandas.pydata.org/pandas-docs/stable/development/extending.html#registering-custom-accessors>`_
219 for more.
220
221 .. currentmodule:: dask.dataframe
222
223 .. autofunction:: dask.dataframe.extensions.register_dataframe_accessor
224 .. autofunction:: dask.dataframe.extensions.register_series_accessor
225 .. autofunction:: dask.dataframe.extensions.register_index_accessor