2

I enountered

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

on calling python code in R using reticulate package in R.

The code in python runs correctly. I am not sure from where this error is coming. I am using pvlib python library to call some in build databases.

My code is for R is:

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

I have issue with cec_inverters = pvlib$pvsystem$retrieve_sam("CECInverter") when I the code in python it's working but running same commands in R is giving me error. I am not sure what the issue is. Please help me resolve this issue.

The similar code in python is:

import pandas as pd
import numpy as np
import pvlib

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

I tried looking for solution but so far didn't find anything useful.

Here's the traceback:

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")
Will Holmgren
  • 696
  • 5
  • 12
Jaws
  • 65
  • 2
  • 8

2 Answers2

2

Try using the as parameter in import(). Your code errored for me as well but this worked:

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")
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • Wow this is truly insane. Do you have any explanation for why this code works but the original fails? The examples from the `reticulate` docs look like OP's code, not like this. – John Zwinck May 06 '18 at 10:24
  • @JohnZwinck many things working like that (strangely). Initially I had problem with `pvlib$pvsystem$retrieve_sam('SandiaMod')` but I uninstalled `reticulate` package and installed again. It was like there was no issue. Now I am having issue with accessing stuff from my databases. `sandia_module = sandia_modules["Canadian_Solar_CS5P_220M___2009_"]` This line of code was working perfectly before with `sandia_modules` but it is giving an Error now: `object of type 'environment' is not subsettable` Not sure why things working so strange. One time they work and other time behaving opposite – Jaws May 06 '18 at 11:01
  • found this solution to my latest issue: "(COPIED): I used [[ rather than [ because the former does single element indexing whereas the latter can select ranges via logical vectors." If that was the case why I was able to access before. Ridiculous.. – Jaws May 06 '18 at 11:23
  • I haven't been able to fully trace what's happening in `reticulate` to cause this but I've seen it with DataFrame ops when other module code has things like https://github.com/pvlib/pvlib-python/blob/2d08caaa5e94c9b4f70de4c267483c47dacae935/pvlib/forecast.py#L7-L8 in it. In reality, only the `pd <- import('pandas', as = "pd")` is rly needed but it also doesn't hurt to mimic the python `import x as y` with the `as = ` `import()` param. – hrbrmstr May 06 '18 at 13:14
0

I figured out this long back but posting here now that "reticulate" package from GitHub is the latest one and many issues has been address within this version. The main one being pandas DataFrame conversion to R data.frame

Here's the code to do so

library(devtools)
devtools::install_github("rstudio/reticulate")
Jaws
  • 65
  • 2
  • 8