Skip to content

DOC: add FAQ section on monkey-patching #2530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions doc/source/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/pydata/pandas/issues/>`_ , 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
Expand Down Expand Up @@ -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