From 8d7cb1dae9d904ed7e8f7fbacbcb73d15eb8bf86 Mon Sep 17 00:00:00 2001 From: y-p Date: Fri, 14 Dec 2012 04:56:52 +0200 Subject: [PATCH] DOC: add FAQ section on monkey-patching --- doc/source/faq.rst | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/doc/source/faq.rst b/doc/source/faq.rst index 2a3620f8ae50c..16c7972bd3fb3 100644 --- a/doc/source/faq.rst +++ b/doc/source/faq.rst @@ -21,6 +21,47 @@ Frequently Asked Questions (FAQ) import matplotlib.pyplot as plt plt.close('all') +.. _ref-monkey-patching: + + +---------------------------------------------------- + +Pandas is a powerful tool and already has a plethora of data manipulation +operations implemented, most of them are very fast as well. +It's very possible however that certain functionality that would make your +life easier is missing. In that case you have several options: + +1) Open an issue on `Github `_ , explain your need and the sort of functionality you would like to see implemented. +2) Fork the repo, Implement the functionality yourself and open a PR + on Github. +3) Write a method that performs the operation you are interested in and + Monkey-patch the pandas class as part of your IPython profile startup + or PYTHONSTARTUP file. + + For example, here is an example of adding an ``just_foo_cols()`` + method to the dataframe class: + +.. ipython:: python + + import pandas as pd + def just_foo_cols(self): + """Get a list of column names containing the string 'foo' + + """ + return [x for x in self.columns if 'foo' in x] + + pd.DataFrame.just_foo_cols = just_foo_cols # monkey-patch the DataFrame class + df = pd.DataFrame([range(4)],columns= ["A","foo","foozball","bar"]) + df.just_foo_cols() + del pd.DataFrame.just_foo_cols # you can also remove the new method + + +Monkey-patching is usually frowned upon because it makes your code +less portable and can cause subtle bugs in some circumstances. +Monkey-patching existing methods is usually a bad idea in that respect. +When used with proper care, however, it's a very useful tool to have. + + .. _ref-scikits-migration: Migrating from scikits.timeseries to pandas >= 0.8.0 @@ -171,5 +212,3 @@ interval (``'start'`` or ``'end'``) convention: data = Series(np.random.randn(50), index=rng) resampled = data.resample('A', kind='timestamp', convention='end') resampled.index - -