i want use pandas2ri.py2ri(df) ro replace r_dataframe = com.convert_to_r_dataframe(df) for my data,i find the old function can work well but the pyr2 report error message like:
NotImplementedError
Traceback (most recent call last) <ipython-input-40-5278a5dfa2ad> in <module>()
18
19 # r_dataframe = com.convert_to_r_dataframe(data)
---> 20 r_datafrmae=pandas2ri.py2ri(data)
21
22 # type(r_dataframe)
/usr/local/lib/python2.7/dist-packages/singledispatch.pyc in wrapper(*args, **kw)
208
209 def wrapper(*args, **kw):
--> 210 return dispatch(args[0].__class__)(*args, **kw)
211
212 registry[object] = func
/usr/local/lib/python2.7/dist-packages/rpy2-2.7.2-py2.7-linux-x86_64.egg/rpy2/robjects/pandas2ri.pyc in py2ri_pandasdataframe(obj)
42 od[name] = StrVector(values)
43 else:
---> 44 od[name] = conversion.py2ri(values)
45 return DataFrame(od)
46
/usr/local/lib/python2.7/dist-packages/singledispatch.pyc in wrapper(*args, **kw)
208
209 def wrapper(*args, **kw):
--> 210 return dispatch(args[0].__class__)(*args, **kw)
211
212 registry[object] = func
/usr/local/lib/python2.7/dist-packages/rpy2-2.7.2-py2.7-linux-x86_64.egg/rpy2/robjects/conversion.pyc in _py2ri(obj)
58 (ri) objects.
59 """
---> 60 raise NotImplementedError("Conversion 'py2ri' not defined for objects of type '%s'" % str(type(obj)))
61
62 def _py2ro(obj):
NotImplementedError: Conversion 'py2ri' not defined for objects of type '<class 'pandas.core.series.Series'>'
the data type is pandas.core.frame.DataFrame,so i can not understand the error .
Comment From: jorisvandenbossche
@feixialong Thanks for the report. Can you provide a reproducible example? A small code snippet that constructs a dataframe and that shows this error?
Comment From: feixialong
@jorisvandenbossche in my computer this code example will cause an error: import pandas as pd import numpy as np from rpy2.robjects import pandas2ri from rpy2.robjects import r
a=r('iris') b=pandas2ri.ri2py(a) pandas2ri.py2ri(b)
Comment From: lgautier
@feixialong : issues with rpy2
should be reported on the relevant issue tracker: https://bitbucket.org/rpy2/rpy2/issues?status=new&status=open
Now for this time (and for this time only ;-) ) I will answer here.
Additional conversion schemes must be "activated" in order to be used as a new global default conversion. This can be achieved with:
pandas2ri.activate()
One can also consider the use of a local converter (the conversion can be costly step, sometimes one does not want it to occur all the time):
from rpy2.robjects import default_converter
from rpy2.robjects.conversion import Converter, localconverter
with localconverter(default_converter + pandas2ri.converter) as cv:
c = pandas2ri.py2ri(b)
There is more on converters in the rpy2 documentation: - general doc - http://rpy2.readthedocs.org/en/version_2.7.x/robjects_convert.html - local converter in action with the interface to R's dplyr - http://rpy2.readthedocs.org/en/version_2.7.x/lib_dplyr.html
Comment From: blairg23
Perhaps spelling?
r_datafrmae=pandas2ri.py2ri(data)
should be r_dataframe=pandas2ri.py2ri(data)