-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: add doc on ExtensionArray and extending pandas #19936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
dcd125b
0842fcf
e1f3f60
057a4f9
51097c6
10a25cf
59f83ce
87aca05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,3 +262,38 @@ Data validation | |
|
||
Engarde is a lightweight library used to explicitly state your assumptions abour your datasets | ||
and check that they're *actually* true. | ||
|
||
.. _ecosystem.extensions: | ||
|
||
Extension Data Types | ||
-------------------- | ||
|
||
Pandas provides an interface for defining | ||
:ref:`extension types <extending.extension-types>` to extend NumPy's type | ||
system. The following libraries implement that interface to provide types not | ||
found in NumPy or pandas, which work well with pandas' data containers. | ||
|
||
`cyberpandas`_ | ||
~~~~~~~~~~~~~~ | ||
|
||
Cyberpandas provides an extension type for storing arrays of IP Addresses. These | ||
arrays can be stored inside pandas' Series and DataFrame. | ||
|
||
.. _ecosystem.accessors: | ||
|
||
Accessors | ||
--------- | ||
|
||
A directory of projects providing | ||
:ref:`extension accessors <extending.register-accessors>`. This is for users to | ||
discover new accessors and for libraries authors to coordinate on the namespace. | ||
|
||
============== ========== ================= | ||
Library Accessor Classes | ||
============== ========== ================= | ||
`cyberpandas`_ ``ip`` Series | ||
`pdvega`_ ``vgplot`` Series, DataFrame | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could put double backticks around Series and DataFrame in this table |
||
============== ========== ================= | ||
|
||
.. _cyberpandas: https://cyberpandas.readthedocs.io/en/latest | ||
.. _pdvega: https://jakevdp.github.io/pdvega/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
.. _extending: | ||
|
||
**************** | ||
Extending Pandas | ||
**************** | ||
|
||
While pandas provides a rich set of methods, containers, and data types, your | ||
needs may not be fully satisfied. Pandas offers a few options for extending | ||
pandas. | ||
|
||
.. _extending.register-accessors: | ||
|
||
Registering Custom Accessors | ||
---------------------------- | ||
|
||
Libraries can use the decorators | ||
:func:`pandas.api.extensions.register_dataframe_accessor`, | ||
:func:`pandas.api.extensions.register_series_accessor`, and | ||
:func:`pandas.api.extensions.register_index_accessor`, to add additional | ||
"namespaces" to pandas objects. All of these follow a similar convention: you | ||
decorate a class, providing the name of attribute to add. The class's `__init__` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. `__init__` should have double backticks |
||
method gets the object being decorated. For example: | ||
|
||
.. code-block:: python | ||
|
||
@pd.api.extensions.register_dataframe_accessor("geo") | ||
class GeoAccessor(object): | ||
def __init__(self, pandas_obj): | ||
self._obj = pandas_obj | ||
|
||
@property | ||
def center(self): | ||
# return the geographic center point of this DataFarme | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DataFarme --> DataFrame |
||
lon = self._obj.latitude | ||
lat = self._obj.longitude | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return (float(lon.mean()), float(lat.mean())) | ||
|
||
def plot(self): | ||
# plot this array's data on a map, e.g., using Cartopy | ||
pass | ||
|
||
Now users can access your methods using the `geo` namespace: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. `geo` should have double backticks |
||
|
||
>>> ds = pd.DataFrame({'longitude': np.linspace(0, 10), | ||
... 'latitude': np.linspace(0, 20)}) | ||
>>> ds.geo.center | ||
(5.0, 10.0) | ||
>>> ds.geo.plot() | ||
# plots data on a map | ||
|
||
This can be a convenient way to extend pandas objects without subclassing them. | ||
If you write a custom accessor, make a pull request adding it to our | ||
:ref:`ecosystem` page. | ||
|
||
.. _extending.extension-types: | ||
|
||
Extension Types | ||
--------------- | ||
|
||
Pandas defines an interface for implementing data types and arrays that *extend* | ||
NumPy's type system. Pandas iteself uses the extension system for some types | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. iteself --> itself |
||
that aren't built into NumPy (categorical, period, interval, datetime with | ||
timezone). | ||
|
||
Libraries can define an custom array and data type. When pandas encounters these | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an -> a |
||
objects, they will be handled properly (i.e. not converted to an ndarray of | ||
objects). Many methods like :func:`pandas.isna` will dispatch to the extension | ||
type's implementation. | ||
|
||
If you're building a library that implements the interface, please publicize it | ||
on :ref:`ecosystem.extensions`. | ||
|
||
The interface consists of two classes. | ||
|
||
``ExtensionDtype`` | ||
"""""""""""""""""" | ||
|
||
An ``ExtensionDtype`` is similar to a ``numpy.dtype`` object. It describes the | ||
data type. Implementors are responsible for a few unique items like the name. | ||
|
||
One particularly important item is the ``type`` property. This should be the | ||
class that is the scalar type for your data. For example, if you were writing an | ||
extension array for IP Address data, this might be ``ipaddress.IPv4Address``. | ||
|
||
See ``pandas/core/dtypes/base.py`` for interface definition. | ||
|
||
``ExtensionArray`` | ||
"""""""""""""""""" | ||
|
||
This class provides all the array-like functionality. ExtensionArrays are | ||
limited to 1 dimension. An ExtensionArray is linked to an ExtensionDtype via the | ||
``dtype`` attribute. | ||
|
||
Pandas makes no restrictions on how an extension array is created via its | ||
``__new__`` or ``__init__``, and puts no restrictions on how you store your | ||
data. We do require that your array be convertible to a NumPy array, even if | ||
this is relatively expensive (as it is for ``Categorical``). | ||
|
||
They may be backed by none, one, or many NumPy ararys. For example, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ararys --> arrays |
||
``pandas.Categorical`` is an extension array backed by two arrays, | ||
one for codes and one for categories. An array of IPv6 address may | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. address --> addresses |
||
be backed by a NumPy structured array with two fields, one for the | ||
lower 64 bits and one for the upper 64 bits. Or they may be backed | ||
by some other storage type, like Python lists. | ||
|
||
See ``pandas/core/arrays/base.py`` for the interface definition. The docstrings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can do a direct reference to code here (e.g. the github link) |
||
and comments contain guidance for properly implementing the interface. | ||
|
||
.. _ref-subclassing-pandas: | ||
|
||
Subclassing pandas Data Structures | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could maybe put double backticks around pandas here. Not sure if we want to avoid doing that in section headers though. |
||
---------------------------------- | ||
|
||
.. warning:: There are some easier alternatives before considering subclassing ``pandas`` data structures. | ||
|
||
1. Extensible method chains with :ref:`pipe <basics.pipe>` | ||
|
||
2. Use *composition*. See `here <http://en.wikipedia.org/wiki/Composition_over_inheritance>`_. | ||
|
||
3. Extending by :ref:`registering an accessor <extending.register-accessors>` | ||
|
||
This section describes how to subclass ``pandas`` data structures to meet more specific needs. There are 2 points which need attention: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 -> two |
||
|
||
1. Override constructor properties. | ||
2. Define original properties | ||
|
||
.. note:: You can find a nice example in `geopandas <https://github.com/geopandas/geopandas>`_ project. | ||
|
||
Override Constructor Properties | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Each data structure has constructor properties to specifying data constructors. By overriding these properties, you can retain defined-classes through ``pandas`` data manipulations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the first sentence could be a bit clearer; it doesn't read quite right to me. I realize this is just a section you moved, so could maybe be deferred to a separate PR. |
||
|
||
There are 3 constructors to be defined: | ||
|
||
- ``_constructor``: Used when a manipulation result has the same dimesions as the original. | ||
- ``_constructor_sliced``: Used when a manipulation result has one lower dimension(s) as the original, such as ``DataFrame`` single columns slicing. | ||
- ``_constructor_expanddim``: Used when a manipulation result has one higher dimension as the original, such as ``Series.to_frame()`` and ``DataFrame.to_panel()``. | ||
|
||
Following table shows how ``pandas`` data structures define constructor properties by default. | ||
|
||
=========================== ======================= =================== ======================= | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would leave out Panel entirely. |
||
Property Attributes ``Series`` ``DataFrame`` ``Panel`` | ||
=========================== ======================= =================== ======================= | ||
``_constructor`` ``Series`` ``DataFrame`` ``Panel`` | ||
``_constructor_sliced`` ``NotImplementedError`` ``Series`` ``DataFrame`` | ||
``_constructor_expanddim`` ``DataFrame`` ``Panel`` ``NotImplementedError`` | ||
=========================== ======================= =================== ======================= | ||
|
||
Below example shows how to define ``SubclassedSeries`` and ``SubclassedDataFrame`` overriding constructor properties. | ||
|
||
.. code-block:: python | ||
|
||
class SubclassedSeries(Series): | ||
|
||
@property | ||
def _constructor(self): | ||
return SubclassedSeries | ||
|
||
@property | ||
def _constructor_expanddim(self): | ||
return SubclassedDataFrame | ||
|
||
class SubclassedDataFrame(DataFrame): | ||
|
||
@property | ||
def _constructor(self): | ||
return SubclassedDataFrame | ||
|
||
@property | ||
def _constructor_sliced(self): | ||
return SubclassedSeries | ||
|
||
.. code-block:: python | ||
|
||
>>> s = SubclassedSeries([1, 2, 3]) | ||
>>> type(s) | ||
<class '__main__.SubclassedSeries'> | ||
|
||
>>> to_framed = s.to_frame() | ||
>>> type(to_framed) | ||
<class '__main__.SubclassedDataFrame'> | ||
|
||
>>> df = SubclassedDataFrame({'A', [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) | ||
>>> df | ||
A B C | ||
0 1 4 7 | ||
1 2 5 8 | ||
2 3 6 9 | ||
|
||
>>> type(df) | ||
<class '__main__.SubclassedDataFrame'> | ||
|
||
>>> sliced1 = df[['A', 'B']] | ||
>>> sliced1 | ||
A B | ||
0 1 4 | ||
1 2 5 | ||
2 3 6 | ||
>>> type(sliced1) | ||
<class '__main__.SubclassedDataFrame'> | ||
|
||
>>> sliced2 = df['A'] | ||
>>> sliced2 | ||
0 1 | ||
1 2 | ||
2 3 | ||
Name: A, dtype: int64 | ||
>>> type(sliced2) | ||
<class '__main__.SubclassedSeries'> | ||
|
||
Define Original Properties | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
To let original data structures have additional properties, you should let ``pandas`` know what properties are added. ``pandas`` maps unknown properties to data names overriding ``__getattribute__``. Defining original properties can be done in one of 2 ways: | ||
|
||
1. Define ``_internal_names`` and ``_internal_names_set`` for temporary properties which WILL NOT be passed to manipulation results. | ||
2. Define ``_metadata`` for normal properties which will be passed to manipulation results. | ||
|
||
Below is an example to define 2 original properties, "internal_cache" as a temporary property and "added_property" as a normal property | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 --> two |
||
|
||
.. code-block:: python | ||
|
||
class SubclassedDataFrame2(DataFrame): | ||
|
||
# temporary properties | ||
_internal_names = pd.DataFrame._internal_names + ['internal_cache'] | ||
_internal_names_set = set(_internal_names) | ||
|
||
# normal properties | ||
_metadata = ['added_property'] | ||
|
||
@property | ||
def _constructor(self): | ||
return SubclassedDataFrame2 | ||
|
||
.. code-block:: python | ||
|
||
>>> df = SubclassedDataFrame2({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) | ||
>>> df | ||
A B C | ||
0 1 4 7 | ||
1 2 5 8 | ||
2 3 6 9 | ||
|
||
>>> df.internal_cache = 'cached' | ||
>>> df.added_property = 'property' | ||
|
||
>>> df.internal_cache | ||
cached | ||
>>> df.added_property | ||
property | ||
|
||
# properties defined in _internal_names is reset after manipulation | ||
>>> df[['A', 'B']].internal_cache | ||
AttributeError: 'SubclassedDataFrame2' object has no attribute 'internal_cache' | ||
|
||
# properties defined in _metadata are retained | ||
>>> df[['A', 'B']].added_property | ||
property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libraries -> library