You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/internals.rst
+68-2
Original file line number
Diff line number
Diff line change
@@ -94,8 +94,74 @@ not check (or care) whether the levels themselves are sorted. Fortunately, the
94
94
constructors ``from_tuples`` and ``from_arrays`` ensure that this is true, but
95
95
if you compute the levels and labels yourself, please be careful.
96
96
97
+
.. _ref-composition-pandas:
97
98
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
+
classCompositedSeries(object):
113
+
114
+
def__init__(self, arr, *args, **kwargs):
115
+
self._series = Series(arr, *args, **kwargs)
116
+
117
+
def__getattr__(self, key):
118
+
returngetattr(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
+
returnrepr(self._series)
126
+
127
+
deforiginal_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'])
0 commit comments