Frivolous Musings

Some thoughts on politics/lit/tech/life itself


Monkey-patching Pandas

A common catch for Pandas users is that the default DataFrame.to_csv option is index=True. Indices are often significant in Pandas, but perhaps just as often a simple range of integers $0..n$ adding no new information. You can see some expressions of annoyance about it in this thread, where Joris Van den Bossche (a Pandas core contributor and GeoPandas maintainer) notes that GeoPandas went with a much smarter default of checking if the index is non-standard and only adding it in this case.

Anyway, for me most of the time when casting to CSV I just want to send it to a colleague who will look at it in Excel or something, so I patched it in my $HOME/.ipython/startup.py. The key insight here for me was how to use partial with a class method; for this Python 3.4 added partialmethod.

from functools import partialmethod
import pandas as pd

pd.DataFrame.to_csv = partialmethod(pd.DataFrame.to_csv, index=False)

(If you want this in a regular Python REPL there is also $HOME/.pythonrc.py.)