我遇到了
Error in py_get_attr_impl(x, name, silent) : AttributeError: 'DataFrame' object has no attribute '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")
当我在 python 中运行代码时,我遇到了 cec_inverters = pvlib$pvsystem$retrieve_sam("CECInverter")
的问题,但在 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")
关于python - 属性错误: 'DataFrame' object has no attribute 'dtype' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50197832/