Skip to content

DOC: Add composition example to internals.rst #9984

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion doc/source/internals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,73 @@ not check (or care) whether the levels themselves are sorted. Fortunately, the
constructors ``from_tuples`` and ``from_arrays`` ensure that this is true, but
if you compute the levels and labels yourself, please be careful.

.. _ref-composition-pandas:

Define Original Data Structures using pandas
--------------------------------------------

.. warning:: If you simply want to add some functionality to ``pandas``, the easiest
way is monkey-patching. See :ref:`Adding Features to your pandas Installation <ref-monkey-patching>`.

This section describes how to define your original data structure which extends ``pandas`` functionality using `composition <http://en.wikipedia.org/wiki/Composition_over_inheritance>`_.

Below example shows an original class which is mostly compatible with ``Series``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below example -> The example below

The class have a ``_series`` property to hold standard ``Series`` (composition), and defining ``__getattr__`` to delegate all the undefined properties / methods to it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class has a _series property that holds a standard Series (composition), and defines __getattr__ to delegate all undefined properties and methods to the series.


.. code-block:: python

class CompositedSeries(object):

def __init__(self, arr, *args, **kwargs):
self._series = Series(arr, *args, **kwargs)

def __getattr__(self, key):
return getattr(self._series, key)

def __getitem__(self, key):
# should results in the same class
return CompositedSeries(self._series[key])

def __repr__(self):
return repr(self._series)

def original_method(self):
return 'result'

The class can behave almost the same as standard ``Series``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same as -> the same as a

Note that some operations (such as arithmetic / set operations) will fail because
these are undefined in above example.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in above example -> in this example


.. code-block:: python

>>> s = CompositedSeries([1, 2, 3], index=['A', 'B', 'C'])
>>> s
A 1
B 2
C 3
dtype: int64

>>> type(s)
<class '__main__.CompositedSeries'>

>>> s.dtype
int64

>>> sliced = s[1:3]
>>> sliced
B 2
C 3
dtype: int64

>>> type(sliced)
<class '__main__.CompositedSeries'>

>>> sliced.original_method()
result

>>> sliced + 2
TypeError: unsupported operand type(s) for +: 'CompositedSeries' and 'int'

.. _ref-subclassing-pandas:

Subclassing pandas Data Structures
Expand All @@ -103,7 +170,7 @@ Subclassing pandas Data Structures

1. Monkey-patching: See :ref:`Adding Features to your pandas Installation <ref-monkey-patching>`.

2. Use *composition*. See `here <http://en.wikipedia.org/wiki/Composition_over_inheritance>`_.
2. Use *composition*. See :ref:`Define Original Data Structures using pandas <ref-composition-pandas>`.

This section describes how to subclass ``pandas`` data structures to meet more specific needs. There are 2 points which need attention:

Expand Down