I'm using reticulate to run python code in R Studio. I have a python function that takes a startdate and enddate parameter that I'm trying to call from R Studio. In this function, a dataframe datetime64 column is filtered based on startdate and enddate.
I'm defining the start and end parameters in R as
start<-as.Date("2024-04-01")
end<-as.Date("2024-05-13")
but this is causing the python function to throw an error:
TypeError: Invalid comparison between dtype=datetime64[ns] and date
My question is: is there a way to define a datetime64 in R? Or should I do some messy validation and type conversion in my python function?
full code:
library(reticulate)
#setwd("~/home/pythonstuff/")
source_python("~/home/pythonstuff/things/tracker.py")
pt<-import("tracker")
start<-as.Date("2024-04-01")
end<-as.Date("2024-05-13")
df<-pt$get_all_data(start, end)
df
There may be an option in R and I don't know what the python looks like, but casting in the python is probably the best option. The python script should control types that only matter internally to the script. That way it can be used in other scenarios (even if that's unlikely, it's probably still a good practice).
The conversion can be done somewhat like this:
import numpy as np
def get_all_data(start, end):
try:
start, end = np.datetime64(start), np.datetime64(end)
except ValueError as e:
# log something or add info about start/end to the error message
raise ValueError("new message") from e
# Do other stuff
You may also be able to just pass the date strings into your function, but it's probably a better idea to handle the conversion even if it's not strictly.