@@ -61,7 +61,7 @@ Extension Types
61
61
62
62
.. warning ::
63
63
64
- The :class: `pandas.api.extension .ExtensionDtype ` and :class: `pandas.api.extension .ExtensionArray ` APIs are new and
64
+ The :class: `pandas.api.extensions .ExtensionDtype ` and :class: `pandas.api.extensions .ExtensionArray ` APIs are new and
65
65
experimental. They may change between versions without warning.
66
66
67
67
Pandas defines an interface for implementing data types and arrays that *extend *
@@ -79,10 +79,10 @@ on :ref:`ecosystem.extensions`.
79
79
80
80
The interface consists of two classes.
81
81
82
- :class: `~pandas.api.extension .ExtensionDtype `
82
+ :class: `~pandas.api.extensions .ExtensionDtype `
83
83
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84
84
85
- A :class: `pandas.api.extension .ExtensionDtype ` is similar to a ``numpy.dtype `` object. It describes the
85
+ A :class: `pandas.api.extensions .ExtensionDtype ` is similar to a ``numpy.dtype `` object. It describes the
86
86
data type. Implementors are responsible for a few unique items like the name.
87
87
88
88
One particularly important item is the ``type `` property. This should be the
@@ -91,7 +91,7 @@ extension array for IP Address data, this might be ``ipaddress.IPv4Address``.
91
91
92
92
See the `extension dtype source `_ for interface definition.
93
93
94
- :class: `~pandas.api.extension .ExtensionArray `
94
+ :class: `~pandas.api.extensions .ExtensionArray `
95
95
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96
96
97
97
This class provides all the array-like functionality. ExtensionArrays are
@@ -113,6 +113,54 @@ by some other storage type, like Python lists.
113
113
See the `extension array source `_ for the interface definition. The docstrings
114
114
and comments contain guidance for properly implementing the interface.
115
115
116
+ .. _extending.extension.operator :
117
+
118
+ :class: `~pandas.api.extensions.ExtensionArray ` Operator Support
119
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
120
+
121
+ .. versionadded :: 0.24.0
122
+
123
+ By default, there are no operators defined for the class :class: `~pandas.api.extensions.ExtensionArray `.
124
+ There are two approaches for providing operator support for your ExtensionArray:
125
+
126
+ 1. Define each of the operators on your ``ExtensionArray `` subclass.
127
+ 2. Use an operator implementation from pandas that depends on operators that are already defined
128
+ on the underlying elements (scalars) of the ExtensionArray.
129
+
130
+ For the first approach, you define selected operators, e.g., ``__add__ ``, ``__le__ ``, etc. that
131
+ you want your ``ExtensionArray `` subclass to support.
132
+
133
+ The second approach assumes that the underlying elements (i.e., scalar type) of the ``ExtensionArray ``
134
+ have the individual operators already defined. In other words, if your ``ExtensionArray ``
135
+ named ``MyExtensionArray `` is implemented so that each element is an instance
136
+ of the class ``MyExtensionElement ``, then if the operators are defined
137
+ for ``MyExtensionElement ``, the second approach will automatically
138
+ define the operators for ``MyExtensionArray ``.
139
+
140
+ A mixin class, :class: `~pandas.api.extensions.ExtensionScalarOpsMixin ` supports this second
141
+ approach. If developing an ``ExtensionArray `` subclass, for example ``MyExtensionArray ``,
142
+ can simply include ``ExtensionScalarOpsMixin `` as a parent class of ``MyExtensionArray ``,
143
+ and then call the methods :meth: `~MyExtensionArray._add_arithmetic_ops ` and/or
144
+ :meth: `~MyExtensionArray._add_comparison_ops ` to hook the operators into
145
+ your ``MyExtensionArray `` class, as follows:
146
+
147
+ .. code-block :: python
148
+
149
+ class MyExtensionArray (ExtensionArray , ExtensionScalarOpsMixin ):
150
+ pass
151
+
152
+ MyExtensionArray._add_arithmetic_ops()
153
+ MyExtensionArray._add_comparison_ops()
154
+
155
+ Note that since ``pandas `` automatically calls the underlying operator on each
156
+ element one-by-one, this might not be as performant as implementing your own
157
+ version of the associated operators directly on the ``ExtensionArray ``.
158
+
159
+ .. _extending.extension.testing :
160
+
161
+ Testing Extension Arrays
162
+ ^^^^^^^^^^^^^^^^^^^^^^^^
163
+
116
164
We provide a test suite for ensuring that your extension arrays satisfy the expected
117
165
behavior. To use the test suite, you must provide several pytest fixtures and inherit
118
166
from the base test class. The required fixtures are found in
@@ -174,11 +222,11 @@ There are 3 constructor properties to be defined:
174
222
Following table shows how ``pandas `` data structures define constructor properties by default.
175
223
176
224
=========================== ======================= =============
177
- Property Attributes ``Series `` ``DataFrame ``
225
+ Property Attributes ``Series `` ``DataFrame ``
178
226
=========================== ======================= =============
179
- ``_constructor `` ``Series `` ``DataFrame ``
180
- ``_constructor_sliced `` ``NotImplementedError `` ``Series ``
181
- ``_constructor_expanddim `` ``DataFrame `` ``Panel ``
227
+ ``_constructor `` ``Series `` ``DataFrame ``
228
+ ``_constructor_sliced `` ``NotImplementedError `` ``Series ``
229
+ ``_constructor_expanddim `` ``DataFrame `` ``Panel ``
182
230
=========================== ======================= =============
183
231
184
232
Below example shows how to define ``SubclassedSeries `` and ``SubclassedDataFrame `` overriding constructor properties.
0 commit comments