[英]Why am I getting “module 'rpy2.robjects.conversion' has no attribute 'py2rpy'” error?
I'm trying to convert a pandas dataframe into an R dataframe using the guide here .我正在尝试使用此处的指南将 pandas dataframe 转换为 R Z6A8064B5DF479450550053。 The code I have so far is:
我到目前为止的代码是:
import pandas as pd
import rpy2.robjects as ro
from rpy2.robjects import pandas2ri
from rpy2.robjects.conversion import localconverter
pd_df = pd.DataFrame({'int_values': [1, 2, 3],
'str_values': ['abc', 'def', 'ghi']})
with localconverter(ro.default_converter + pandas2ri.converter):
r_from_pd_df = ro.conversion.py2rpy(pd_df)
However, this is giving me the following error:但是,这给了我以下错误:
Traceback (most recent call last):
File <my_file_ref>, line 13, in <module>
r_from_pd_df = ro.conversion.py2rpy(pd_df)
AttributeError: module 'rpy2.robjects.conversion' has no attribute 'py2rpy'
I have found this relevant question where the OP refers to function names being changed however doesn't explain what the changes are.我发现了这个相关问题,其中 OP 指的是 function 名称正在更改,但没有解释更改是什么。 I've tried looking at the module but I'm not quite advanced enough to be able to make sense of it.
我试过看这个模块,但我还不够先进,无法理解它。
The accepted answer refers to checking versions which I've done and I'm definitely using rpy2 v3.3.3 which is the same as the guide I'm following.接受的答案是指检查我已经完成的版本,并且我肯定使用的是 rpy2 v3.3.3,这与我正在遵循的指南相同。
Has anyone encountered this error and found a solution?有没有人遇到这个错误并找到了解决方案?
The section of the rpy2 documentation you are pointing out is built by running the code example.您指出的 rpy2 文档部分是通过运行代码示例构建的。 This is means that the example did work with the corresponding version of rpy2.
这意味着该示例确实适用于相应版本的 rpy2。
I am not sure you are using that version of rpy2 at runtime?我不确定您在运行时使用的是该版本的 rpy2 吗? For example, add
print(rpy2.__version__)
to check that this is the case.例如,添加
print(rpy2.__version__)
以检查是否是这种情况。
For what is worth, the latest release in the rpy2 3.3.x series is 3.3.6 and there is probably no good reason to stay with 3.3.3.值得一提的是,rpy2 3.3.x 系列的最新版本是 3.3.6,可能没有充分的理由继续使用 3.3.3。 Otherwise rpy2 3.4.0 was just released;
否则 rpy2 3.4.0 刚刚发布; if using R 4.0 or greater, or the latest release of the R packages
dplyr
or ggplot2
together with their rpy2 wrapper, I would recommend to use that release. if using R 4.0 or greater, or the latest release of the R packages
dplyr
or ggplot2
together with their rpy2 wrapper, I would recommend to use that release.
[PS: I just tried your example with rpy2-3.4.0 and it runs without error] [PS:我刚刚用 rpy2-3.4.0 试过你的例子,它运行没有错误]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.