0

How to properly convert an R dataframe object to a pandas Dataframe through rpy2? I've tried everything from the documentation and stackoverflow to no avail. My version of rpy2 is 2.9.4 running in Conda.

I've tried:

from rpy2.robjects import pandas2ri
pandas2ri.activate()
import rpy2.robjects as ro
pd_dt = ro.conversion.rpy2py(r_from_pd_df)
print(pd_dt)

where r_from_pd_df is an R dataframe which brings up the error:

AttributeError: module 'rpy2.robjects.conversion' has no attribute 'rpy2py'

Then I used the documentation example:

import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri

from rpy2.robjects.conversion import localconverter
r_df = ro.DataFrame({'int_values': ro.IntVector([1,2,3]),
                     'str_values': ro.StrVector(['abc', 'def', 'ghi'])})
with localconverter(ro.default_converter + pandas2ri.converter):
  pd_from_r_df = ro.conversion.rpy2py(r_df)

that raised the same error.

Mario
  • 561
  • 3
  • 18
  • 2
    Please show us what exactly you tried and what errors you got. – cel Jun 04 '20 at 15:27
  • @cel I added the details – Mario Jun 04 '20 at 15:54
  • `rpy2` is an actively developed project. Did you try the older [version of conversion](https://stackoverflow.com/a/20808449/1422451) which still works for me: `pandas2ri.activate(); py_df = pandas2ri.ri2py(r_df)`? – Parfait Jun 04 '20 at 17:55
  • @Parfait you know what? that's probably the best way to go about doing it – Mario Jun 04 '20 at 20:49
  • 2
    Indeed. Per link above, `rpy2py` is available as of version 3.3.0 (not your version). Note: the answerer is one of the authors and maintainers of `rpy2`. – Parfait Jun 04 '20 at 21:57

0 Answers0