Skip to content

Code Style and Conventions

Phillip Cloud edited this page Jul 28, 2013 · 10 revisions

Python 2/3 Compatibility

Before #4384, pandas depended on the 2to3 tool to ensure that the codebase was Python 2 and 3 compatible. This is not the case anymore. That means that you should be careful about writing code that is Python 2 and Python 3 compatible. To that end, there are new internal functions that abstract away the details of the API changes between Python 2.6 - Python 3.X in pandas.util.compat (which incorporates much of the six module).

range, zip, map, filter, and reduce

range, zip, map, and filter changed from producing lists to iterators in Python 3. For compatibility, you should generally import these functions from pandas.compat, which will mean that they use the iterator form in both Python 2 and Python 3. If you want the list form (i.e., 2.X behavior, you can use lrange, lzip, lmap, and lfilter, which have the same call structure, but wrapped in the list constructor in Python 3. [reduce moved from builtins to functools]

The itertools module: izip, ifilter, imap, etc.

These changed names between Python 2 and Python 3. Just import zip, filter and map from pandas.compat to use them.

iteritems(), itervalues(), iterkeys(), iterlists(), etc.

Python builtins no longer have these methods in Python 3 and are replaced by iterators in items, values, etc.keys(). Whereas the six library maps, for example, six.iteritems to iteritems in 2.X and items(), some pandas objects have iterator methods that are actually different than their equivalents, so pandas.compat tries calling the iter version first and then calls the 3.X version if that fails.

Clone this wiki locally