简体   繁体   English

Python 脚本中的 RPY2 错误! (未为类型为“的对象定义转换‘py2rpy’<class 'pandas.core.frame.dataframe'> ')</class>

[英]RPY2 error in Python script! (Conversion 'py2rpy' not defined for objects of type '<class 'pandas.core.frame.DataFrame'>')

This is my current Python code:这是我当前的 Python 代码:

import pandas as pd
import numpy as np
import rpy2.robjects.packages as rp
from rpy2 import robjects as ro

utils = rp.importr('utils')
utils.chooseCRANmirror(ind=1)
utils.install_packages("sidrar")
sidrar = rp.importr("sidrar")
    
table_NameUrl = pd.read_csv("Data.txt", sep=";", header= None)
table_Size = len(table_NameUrl)
sequence = np.arange(1, table_Size, 2)
for i in sequence:
 csvName = table_NameUrl.iloc[::2]
 csvUrl = table_NameUrl.iloc[1::2]
 last_url = "/n3/all/n6/in%20n3%2028"
 fileName = (tableName + ".csv")
 url = (csvUrl + last_url)
 ro.globalenv['fileName'] = fileName
 ro.globalenv['url'] = url
 ro.r('table= get_sidra(api= url)')
 ro.r('write.csv(table, file= fileName, row.names = F)

Short version of CSV (line by line): CSV 的简短版本(逐行):

Panu 2.1.1, 2.1.5帕努 2.1.1、2.1.5

/t/2093/p/2000,2010/v/93/c86/2776,2777,2778,2779,2780,2781/c2/0,4,5/c1/1,2/ /t/2093/p/2000,2010/v/93/c86/2776,2777,2778,2779,2780,2781/c2/0,4,5/c1/1,2/

Panu 5.1.1, 5.1.2, 5.1.3, 5.1.4, 5.1.5, 5.1.6, 5.1.7, 5.1.9, 5.1.10帕努 5.1.1、5.1.2、5.1.3、5.1.4、5.1.5、5.1.6、5.1.7、5.1.9、5.1.10

/t/1612/p/2013,2014,2015,2016,2017/v/109,216,214/c81/2688,2691,2692,2694,2696,2708,2702,2715,2703/ /t/1612/p/2013,2014,2015,2016,2017/v/109,216,214/c81/2688,2691,2692,2694,2696,2708,2702,2715,2703/

Panu 5.1.8帕努 5.1.8

/t/839/p/2013,2014,2015,2016,2017/v/109,216,214/c81/114254/ /t/839/p/2013,2014,2015,2016,2017/v/109,216,214/c81/114254/

I´m using rpy2 in Python to get R's sidrar package.我在 Python 中使用 rpy2 来获取 R 的sidrar package。

I'm not using native Python packages sidrapy or DadosAbertosBrasil because I need to auto generate a lot of CSVs with the Sidra API's URLs from Data.txt .我没有使用原生 Python 包sidrapyDadosAbertosBrasil ,因为我需要使用来自Data.txt的 Sidra API 的 URL 自动生成大量 CSV。 So, sidrar is the best one to do it.所以, sidrar是最好的选择。

But when I try to use new CSV's name inside of fileName and new CSV's url inside of url this error appears:但是,当我尝试在fileName中使用新的 CSV 名称并在 url 中使用新的 CSV 名称url时,会出现此错误:

Conversion 'py2rpy' not defined for objects of type '<class 'pandas.core.frame.DataFrame'>'

PS: last_url is a url complement to get full Sidra API table, so that's why I need to concatenate the strings. PS: last_url是 url 的补充,以获得完整的 Sidra API 表,所以这就是我需要连接字符串的原因。


Solution: I added this function below in my Python code and the error is gone.解决方案:我在我的 Python 代码中添加了下面这个 function,错误消失了。

from rpy2.robjects.conversion import localconverter as lc

with lc(ro.default_converter + pr.converter):
  fileName_c = ro.conversion.py2rpy(fileName)
  url_c = ro.conversion.py2rpy(url)
ro.globalenv['fileName'] = fileName_c
ro.globalenv['url'] = url_c

This fixed the error.这修复了错误。 It seems to call an R function on pandas dataframe we have to first import the pandas2ri object from rpy2.它似乎在 pandas dataframe 上调用 R function 我们必须首先从 rpy2 导入 pandas2ri object。

from rpy2.robjects import pandas2ri


pandas2ri.activate()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 中的 Rpy2 错误 - 转换 'py2rpy' 未为类型 ' 的对象定义<class 'str'></class> - Rpy2 Error in Django - Conversion 'py2rpy' not defined for objects of type '<class 'str'> Python 错误 - NotImplementedError: 转换 'py2rpy' 没有为类型的对象定义<class 'ipywidgets.widgets.widget_string.text'> '</class> - Python Error - NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'ipywidgets.widgets.widget_string.Text'>' 为什么我收到“模块'rpy2.robjects.conversion'没有属性'py2rpy'”错误? - Why am I getting “module 'rpy2.robjects.conversion' has no attribute 'py2rpy'” error? NotImplementedError: 转换 'rpy2py' 没有为类型 ' 的对象定义<class 'rpy2.rinterface.sexpclosure'> ' 只有在我运行代码两次之后</class> - NotImplementedError: Conversion 'rpy2py' not defined for objects of type '<class 'rpy2.rinterface.SexpClosure'>' only after I run the code twice rpy2在ubuntu上测试获取“ NotImplementedError:从rpy2 DataFrame转换为熊猫的DataFrame” - rpy2 tests getting “NotImplementedError: Conversion from rpy2 DataFrame to pandas' DataFrame” on ubuntu rpy2 转换为 Pandas DataFrame 返回 numpy recarray 而不是 Pandas - rpy2 conversion to pandas DataFrame returns numpy recarray instead of pandas 从 rpy2 dataframe 到 pandas dataframe 的转换不适用于字符串类型的列 - Conversion from rpy2 dataframe to pandas dataframe not working for columns of string type 为 rpy2 转换 python 对象 - Converting python objects for rpy2 使用 rpy2 将 python dataframe 字符列转换为 r - Conversion python dataframe character column to r with rpy2 没有为对象类型命名为 1 的轴<class 'pandas.core.frame.DataFrame'> - No axis named 1 for object type <class 'pandas.core.frame.DataFrame'>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM