pandas 0.7.0 documentation

rpy2 / R interface

Note

This is all highly experimental. I would like to get more people involved with building a nice RPy2 interface for pandas

If your computer has R and rpy2 (> 2.2) installed (which will be left to the reader), you will be able to leverage the below functionality. On Windows, doing this is quite an ordeal at the moment, but users on Unix-like systems should find it quite easy. As a general rule, I would recommend using the latest revision of rpy2 from bitbucket:

# if installing for the first time
hg clone http://bitbucket.org/lgautier/rpy2

cd rpy2
hg pull
hg update
sudo python setup.py install

Note

To use R packages with this interface, you will need to install them inside R yourself. At the moment it cannot install them for you.

Once you have done installed R and rpy2, you should be able to import pandas.rpy.common without a hitch.

Transferring R data sets into Python

The load_data function retrieves an R data set and converts it to the appropriate pandas object (most likely a DataFrame):

In [756]: import pandas.rpy.common as com

In [757]: infert = com.load_data('infert')

In [758]: infert.head()
Out[758]: 
  education  age  parity  induced  case  spontaneous  stratum  pooled.stratum
1    0-5yrs   26       6        1     1            2        1               3
2    0-5yrs   42       1        1     1            0        2               1
3    0-5yrs   39       6        2     1            0        3               4
4    0-5yrs   34       4        2     1            0        4               2
5   6-11yrs   35       3        1     1            1        5              32

Calling R functions with pandas objects

High-level interface to R estimators