Converting DataFrame from R to Python not Working

Issue #615 new
Phi Hoang created an issue

I copy and pasted Laurent’s code in, but I am getting an error back. Do you have any suggestions to fix this error that says “Converter” object has no attribute rpy2py? Many thanks

import pandas as pd
from rpy2 import robjects
from rpy2.robjects import pandas2ri
from rpy2.robjects.conversion import localconverter, Converter

df = pd.DataFrame([['2019-01-01', 1]])
df[0] = pd.to_datetime(df[0])

with localconverter(robjects.default_converter + pandas2ri.converter):
    robjects.globalenv['mydata'] = df

# Only use the default converter
r_datecol = robjects.globalenv['mydata'][0]

# The column with a date was converter to an R vector of dates in the data dataframe:  
# >>> r_datecol  
# R object with classes: ('POSIXct', 'POSIXt') mapped to:
# [2019-01-01]

# Use default + pandas converters
with localconverter(robjects.default_converter + pandas2ri.converter) as cv:
    py_datecol = cv.rpy2py(r_datecol)

# The conversion of the R vector back to a pandas array of dates appears to be working:
# >>> py_datecol                                                               
# DatetimeIndex(['2019-01-01 00:00:00-05:00'], dtype='datetime64[ns, America/New_York]', freq=None)

# The issue is therefore with what is happening at the DataFrame level. Somehow the POSIXct column
# in the R data.frame is mapped to an array of floats (which is the C-level type in R, and should
# only happen at the `rpy2.rinterface` level) rather that to the R-level class for date vectors.
# The issue reported is broader than only date/time vectors.

Comments (0)

  1. Log in to comment