AttributeError: 'DataFrame' object has no attribute 'dtype'

AttributeError: 'DataFrame' object has no attribute 'dtype'

我遇到了

Error in py_get_attr_impl(x, name, silent) : AttributeError: 'DataFrame' object has no attribute 'dtype'

关于使用 R 中的网状包在 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")

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

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")

我很久以前就知道了,但现在在这里发帖说 GitHub 的 "reticulate" 包是最新的,许多问题已经在这个版本中得到解决。主要的是 pandas DataFrame 转换为 R data.frame

这是执行此操作的代码

library(devtools)
devtools::install_github("rstudio/reticulate")