pandas2ri.py2ri on basic DataFrame: falls back to string conversion and raises warning

Issue #394 invalid
Joris Van den Bossche
created an issue

Using a simple pandas DataFrame (no special index, just some integers in a column), conversion with pandas2ri.py2ri seems to 'fail' (it raises a warning, falls back to string conversion and the result holds Factors):

import pandas as pd
from rpy2.robjects import pandas2ri

df = pd.DataFrame({'colA':[1,2,3]})
>>> r_df = pandas2ri.py2ri(df)
/home/joris/miniconda3/envs/dev/lib/python3.5/site-packages/rpy2/robjects/pandas2ri.py:57:
UserWarning: Error while trying to convert the column "colA".
Fall back to string conversion. The error is: Conversion 'py2ri' not defined for
objects of type '<class 'pandas.core.series.Series'>'
  (name, str(e)))

>>> r_df
R object with classes: ('data.frame',) mapped to:
<DataFrame - Python:0x7fd394dd14c8 / R:0x4269358>
[FactorVector]
  colA: <class 'rpy2.robjects.vectors.FactorVector'>
  R object with classes: ('factor',) mapped to:
<FactorVector - Python:0x7fd39407c808 / R:0x30e2048>
[       1,        2,        3]

>>> print(r_df)
  colA
1    1
2    2
3    3

Using Python 3.5, pandas 0.19.2 and rpy2 2.8.5

Comments (8)

  1. Laurent Gautier

    You are holding it wrong. ;-)

    Conversion rules can have interdependencies. Either "activate" the conversion rule globally or use a conversion context manager

    pandas2ri.activate()
    r_df = pandas2ri.py2ri(df)
    

    (tried with the following environment:

    $ docker run --rm -it rpy2/rpy2:2.8.x ipython
    

    )

  2. Joris Van den Bossche reporter

    Whoops, didn't follow my own written documentation .. :-) (http://pandas.pydata.org/pandas-docs/stable/r_interface.html#updating-your-code-to-use-rpy2-functions)

    By the way, the documentation on pandas2ri in the rpy2 docs is rather scarce (not to say, non-existant). I suppose you probably should read on the general conversion interface and machinery, and then you understand the pandas2ri as well. But for people just wanting to convert a dataframe, this may be a big step.

  3. Laurent Gautier

    By the way, the documentation on pandas2ri in the rpy2 docs is rather scarce (not to say, non-existant). I suppose you probably should read on the general conversion interface and machinery, and then you understand the pandas2ri as well. But for people just wanting to convert a dataframe, this may be a big step.

    Fair point. There is an example about it but buried in how to use dplyr: http://rpy2.readthedocs.io/en/version_2.8.x/generated_rst/dplyr.html

    There should probably be a section "rpy2 for pandas users", and since the converter is on my side of fence so is doc. :/ I am gladly taking pull requests.

  4. Joris Van den Bossche reporter

    To further feed my confusion, for doing a conversion the other way around (pandas2ri.ri2py), it is apparently not needed to activate pandas2ri:

    In [6]: from rpy2.robjects import r, pandas2ri
    
    In [7]: r.data('iris')
    Out[7]: 
    R object with classes: ('character',) mapped to:
    <StrVector - Python:0x7f25fd7820c8 / R:0x302b6c8>
    ['iris']
    
    In [8]: pandas2ri.ri2py(r('iris'))
    Out[8]: 
         Sepal.Length  Sepal.Width  Petal.Length  Petal.Width    Species
    1             5.1          3.5           1.4          0.2     setosa
    2             4.9          3.0           1.4          0.2     setosa
    3             4.7          3.2           1.3          0.2     setosa
    ...
    
  5. Log in to comment