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.