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
+73-2
Original file line number
Diff line number
Diff line change
@@ -94,8 +94,79 @@ 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 functionalities 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`` functionalities 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 ``__getattribute__`` 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__getattribute__(self, key):
118
+
try:
119
+
# try to use its own attributes first
120
+
returnobject.__getattribute__(self, key)
121
+
exceptAttributeError:
122
+
# if not found, use Series attribute
123
+
returngetattr(self._series, key)
124
+
125
+
def__getitem__(self, key):
126
+
# should results in the same class
127
+
return CompositedSeries(self._series[key])
128
+
129
+
def__repr__(self):
130
+
returnrepr(self._series)
131
+
132
+
deforiginal_method(self):
133
+
return'result'
134
+
135
+
The class can behave almost the same as standard ``Series``.
136
+
Note that some operations (such as arithmetic / set operations) will fail because
137
+
these are undefined in above example.
138
+
139
+
.. code-block:: python
140
+
141
+
>>> s = CompositedSeries([1, 2, 3], index=['A', 'B', 'C'])
0 commit comments