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
+152
Original file line number
Diff line number
Diff line change
@@ -95,3 +95,155 @@ 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
97
98
+
.. _ref-subclassing-pandas:
99
+
100
+
Subclassing pandas Data Structures
101
+
----------------------------------
102
+
103
+
.. warning:: There are some easier alternatives before considering subclassing ``pandas`` data structures.
104
+
105
+
1. Monkey-patching: See :ref:`Adding Features to your pandas Installation <ref-monkey-patching>`.
106
+
107
+
2. Use *composition*. See `here <http://en.wikipedia.org/wiki/Composition_over_inheritance>`_.
108
+
109
+
This section describes how to subclass ``pandas`` data structures to meet more specific needs. There are 2 points which need attention:
110
+
111
+
1. Override constructor properties.
112
+
2. Define original properties
113
+
114
+
.. note:: You can find a nice example in `geopandas <https://github.com/geopandas/geopandas>`_ project.
115
+
116
+
Override Constructor Properties
117
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118
+
119
+
Each data structure has constructor properties to specifying data constructors. By overriding these properties, you can retain defined-classes through ``pandas`` data manipulations.
120
+
121
+
There are 3 constructors to be defined:
122
+
123
+
- ``_constructor``: Used when a manipulation result has the same dimesions as the original.
124
+
- ``_constructor_sliced``: Used when a manipulation result has one lower dimension(s) as the original, such as ``DataFrame`` single columns slicing.
125
+
- ``_constructor_expanddim``: Used when a manipulation result has one higher dimension as the original, such as ``Series.to_frame()`` and ``DataFrame.to_panel()``.
126
+
127
+
Following table shows how ``pandas`` data structures define constructor properties by default.
To let original data structures have additional properties, you should let ``pandas`` knows what properties are added. ``pandas`` maps unknown properties to data names overriding ``__getattribute__``. Defining original properties can be done either ways:
203
+
204
+
1. Define ``_internal_names`` and ``_internal_names_set`` for temporary properties which WILL NOT be passed to manipulation results.
205
+
2. Define ``_metadata`` for normal properties which will be passed to manipulation results.
206
+
207
+
Below is an example to define 2 original properties, "internal_cache" as a temporary property and "added_property" as a normal property
0 commit comments