1

我认识了

py_get_attr_impl(x,name,silent)中的错误:AttributeError:'DataFrame'对象没有属性'dtype'

在 R 中使用 reticulate 包在 R 中调用 python 代码。

python中的代码运行正常。我不确定这个错误来自哪里。我正在使用 pvlib python 库在构建数据库中调用一些。

我的代码是 R 是:

library('reticulate')
pd <- import('pandas')
pvlib <- import('pvlib')
np <- import('numpy') 
sandia_modules = pvlib$pvsystem$retrieve_sam('SandiaMod')
cec_inverters = pvlib$pvsystem$retrieve_sam("CECInverter")

cec_inverters = pvlib$pvsystem$retrieve_sam("CECInverter")当我在 python 中运行代码时遇到问题,但在 R 中运行相同的命令却给了我错误。我不确定问题是什么。请帮我解决这个问题。

python中类似的代码是:

import pandas as pd
import numpy as np
import pvlib

sandia_modules = pvlib.pvsystem.retrieve_sam('SandiaMod')
cec_inverters = pvlib.pvsystem.retrieve_sam('cecinverter')

我试图寻找解决方案,但到目前为止没有发现任何有用的东西。

这是回溯:

10: stop(list(message = "AttributeError: 'DataFrame' object has no attribute 'dtype'", 
        call = py_get_attr_impl(x, name, silent), cppstack = list(
            file = "", line = -1L, stack = "C++ stack not available on this system")))
9: .Call(`_reticulate_py_get_attr_impl`, x, name, silent)
8: py_get_attr_impl(x, name, silent)
7: py_get_attr(x, name)
6: `$.python.builtin.object`(x[[column]], "dtype")
5: x[[column]]$dtype
4: py_to_r(x[[column]]$dtype$name)
3: py_to_r.pandas.core.frame.DataFrame(result)
2: py_to_r(result)
1: pvlib$pvsystem$retrieve_sam("CECInverter")
4

2 回答 2

2

尝试使用中的as参数import()。您的代码也对我出错,但这有效:

library(reticulate) # version 1.6

pd <- import('pandas', as = "pd")
pvlib <- import('pvlib', as = "pvlib")
np <- import('numpy', as = "np") 

sandia_modules <- pvlib$pvsystem$retrieve_sam('SandiaMod')
cec_inverters <- pvlib$pvsystem$retrieve_sam("CECInverter")
于 2018-05-06T10:10:25.110 回答
0

我很久以前就知道了,但是现在在这里发布"reticulate"来自 GitHub 的包是最新的包,并且在这个版本中已经解决了许多问题。主要是pandas DataFrame转换为 Rdata.frame

这是执行此操作的代码

library(devtools)
devtools::install_github("rstudio/reticulate")
于 2018-06-19T12:57:47.623 回答