Converting python objects for rpy2

試著忘記壹切 提交于 2019-11-27 03:31:41
unutbu

You need to add

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

See http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html:

That import alone is sufficient to switch an automatic conversion of numpy objects into rpy2 objects.

Why make this an optional import, while it could have been included in the function py2ri() (as done in the original patch submitted for that function) ?

Although both are valid and reasonable options, the design decision was taken in order to decouple rpy2 from numpy the most, and do not assume that having numpy installed automatically meant that a programmer wanted to use it.

edit: With the rpy2 series 2.2.x, the import alone is no longer sufficient. The conversion needs to be explicitly activated.

For rpy2 2.2.4 I had to add:

import rpy2.robjects.numpy2ri
rpy2.robjects.numpy2ri.activate()

For me (2.2.1) the following also worked (as documented on http://rpy.sourceforge.net/rpy2/doc-2.2/html/numpy.html):

import rpy2.robjects as ro
from rpy2.robjects.numpy2ri import numpy2ri
ro.conversion.py2ri = numpy2ri
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!