简体   繁体   中英

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 . 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. 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.

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. This is means that the example did work with the corresponding version of rpy2.

I am not sure you are using that version of rpy2 at runtime? For example, add print(rpy2.__version__) to check that this is the case.

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. Otherwise rpy2 3.4.0 was just released; 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM