Skip to content

Commit e85c550

Browse files
committed
DOC: Add composition example to internals.rst
1 parent 82387a6 commit e85c550

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

doc/source/internals.rst

+68-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,73 @@ not check (or care) whether the levels themselves are sorted. Fortunately, the
9494
constructors ``from_tuples`` and ``from_arrays`` ensure that this is true, but
9595
if you compute the levels and labels yourself, please be careful.
9696

97+
.. _ref-composition-pandas:
98+
99+
Define Original Data Structures using pandas
100+
--------------------------------------------
101+
102+
.. warning:: If you simply want to add some functionality to ``pandas``, the easiest
103+
way is monkey-patching. See :ref:`Adding Features to your pandas Installation <ref-monkey-patching>`.
104+
105+
This section describes how to define your original data structure which extends ``pandas`` functionality using `composition <http://en.wikipedia.org/wiki/Composition_over_inheritance>`_.
106+
107+
Below example shows an original class which is mostly compatible with ``Series``.
108+
The class have a ``_series`` property to hold standard ``Series`` (composition), and defining ``__getattr__`` to delegate all the undefined properties / methods to it.
109+
110+
.. code-block:: python
111+
112+
class CompositedSeries(object):
113+
114+
def __init__(self, arr, *args, **kwargs):
115+
self._series = Series(arr, *args, **kwargs)
116+
117+
def __getattr__(self, key):
118+
return getattr(self._series, key)
119+
120+
def __getitem__(self, key):
121+
# should results in the same class
122+
return CompositedSeries(self._series[key])
123+
124+
def __repr__(self):
125+
return repr(self._series)
126+
127+
def original_method(self):
128+
return 'result'
129+
130+
The class can behave almost the same as standard ``Series``.
131+
Note that some operations (such as arithmetic / set operations) will fail because
132+
these are undefined in above example.
133+
134+
.. code-block:: python
135+
136+
>>> s = CompositedSeries([1, 2, 3], index=['A', 'B', 'C'])
137+
>>> s
138+
A 1
139+
B 2
140+
C 3
141+
dtype: int64
142+
143+
>>> type(s)
144+
<class '__main__.CompositedSeries'>
145+
146+
>>> s.dtype
147+
int64
148+
149+
>>> sliced = s[1:3]
150+
>>> sliced
151+
B 2
152+
C 3
153+
dtype: int64
154+
155+
>>> type(sliced)
156+
<class '__main__.CompositedSeries'>
157+
158+
>>> sliced.original_method()
159+
result
160+
161+
>>> sliced + 2
162+
TypeError: unsupported operand type(s) for +: 'CompositedSeries' and 'int'
163+
97164
.. _ref-subclassing-pandas:
98165
99166
Subclassing pandas Data Structures
@@ -103,7 +170,7 @@ Subclassing pandas Data Structures
103170
104171
1. Monkey-patching: See :ref:`Adding Features to your pandas Installation <ref-monkey-patching>`.
105172
106-
2. Use *composition*. See `here <http://en.wikipedia.org/wiki/Composition_over_inheritance>`_.
173+
2. Use *composition*. See :ref:`Define Original Data Structures using pandas <ref-composition-pandas>`.
107174
108175
This section describes how to subclass ``pandas`` data structures to meet more specific needs. There are 2 points which need attention:
109176

0 commit comments

Comments
 (0)