This is my current Python code:
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):
Panu 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/
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
/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
/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.
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
. So, sidrar
is the best one to do it.
But when I try to use new CSV's name inside of fileName
and new CSV's url inside of url
this error appears:
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.
Solution: I added this function below in my Python code and the error is gone.
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.
from rpy2.robjects import pandas2ri
pandas2ri.activate()
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.