I want to pass a datetime array to a Numba function (which cannot be vectorised and would otherwise be very slow). I understand Numba supports numpy.datetime64. However, it seems it supports datetime64[D]
(day precision) but not datetime64[ns]
(nanosecond precision) (I learnt this the hard way: is it documented?).
I tried to convert from datetime64[ns]
to datetime64[D]
, but can't seem to find a way!
I have summarised my problem with the minimal code below. If you run testdf(mydates)
, which is datetime64[D], it works fine. If you run testdf(dates_input)
, which is datetime64[ns], it doesn't. Note that this example simply passes the dates to the Numba function, which doesn't (yet) do anything with them. I try to convert dates_input
to datetime64[D]
, but the conversion doesn't work. In my original code I read from a SQL table into a pandas dataframe, and need a column which changes the day of each date to the 15th.
import numba
import numpy as np
import pandas as pd
import datetime
mydates =np.array(['2010-01-01','2011-01-02']).astype('datetime64[D]')
df=pd.DataFrame()
df["rawdate"]=mydates
df["month_15"] = df["rawdate"].apply(lambda r: datetime.date( r.year, r.month,15 ) )
dates_input = df["month_15"].astype('datetime64[D]')
print dates_input.dtype # Why datetime64[ns] and not datetime64[D] ??
@numba.jit(nopython=True)
def testdf(dates):
return 1
print testdf(mydates)
The error I get if I run testdf(dates_input)
is:
numba.typeinfer.TypingError: Failed at nopython (nopython frontend)
Var 'dates' unified to object: dates := {pyobject}