1 year ago

#352968

test-img

Augustin

How to deal with pandas.DatetimeIndex with reticulate?

I want to use the reticulate package to call a Python function whose first argument time is supposed to be a pandas.DatetimeIndex object. In the body of this python function there is a np.unique(time.year).

Within R, I call this function using a POSIXct vector. However, this vector is converted by reticulate to a np.ndarray. Therefore I get the error

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  AttributeError: 'numpy.ndarray' object has no attribute 'year' 

Example

Python code:

def year_(time: pandas.DatetimeIndex):
  return(time.year)

R code:

library(lubridate)
library(reticulate)

vec_dates <- c(now(), now() + dminutes(10))
source_python("year_.py")
year_(vec_dates)

What kind of R object should I use for reticulate to understand that this object must be converted to pandas.DatetimeIndex? I tried something with rownames but these are converted to a string index.

python

r

pandas

datetime

reticulate

0 Answers

Your Answer

Accepted video resources