@@ -61,7 +61,7 @@ Extension Types
6161
6262.. warning ::
6363
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
6565 experimental. They may change between versions without warning.
6666
6767Pandas defines an interface for implementing data types and arrays that *extend *
@@ -79,10 +79,10 @@ on :ref:`ecosystem.extensions`.
7979
8080The interface consists of two classes.
8181
82- :class: `~pandas.api.extension .ExtensionDtype `
82+ :class: `~pandas.api.extensions .ExtensionDtype `
8383^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8484
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
8686data type. Implementors are responsible for a few unique items like the name.
8787
8888One 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``.
9191
9292See the `extension dtype source `_ for interface definition.
9393
94- :class: `~pandas.api.extension .ExtensionArray `
94+ :class: `~pandas.api.extensions .ExtensionArray `
9595^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9696
9797This class provides all the array-like functionality. ExtensionArrays are
@@ -113,6 +113,54 @@ by some other storage type, like Python lists.
113113See the `extension array source `_ for the interface definition. The docstrings
114114and comments contain guidance for properly implementing the interface.
115115
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+
116164We provide a test suite for ensuring that your extension arrays satisfy the expected
117165behavior. To use the test suite, you must provide several pytest fixtures and inherit
118166from the base test class. The required fixtures are found in
@@ -174,11 +222,11 @@ There are 3 constructor properties to be defined:
174222Following table shows how ``pandas `` data structures define constructor properties by default.
175223
176224=========================== ======================= =============
177- Property Attributes ``Series `` ``DataFrame ``
225+ Property Attributes ``Series `` ``DataFrame ``
178226=========================== ======================= =============
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 ``
182230=========================== ======================= =============
183231
184232Below example shows how to define ``SubclassedSeries `` and ``SubclassedDataFrame `` overriding constructor properties.
0 commit comments