How can I create a datetime64[D] in numba

Viewed 243

I need to pass dates into numba function.

Passing them in as .astype('datetime64[D]') works well. But I need to create an epoch date inside function too.

import numpy as np
import pandas as pd
import numba
from numba import jit
from datetime import datetime, timedelta

def datetime_range(start, end, delta):
    current = start
    while current < end:
        yield current
        current += delta



@jit(nopython=True)
def myfunc(dts):
    epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
    if epoch == dts[0]:
        n = 1
    return epoch


dts = [dt for dt in
       datetime_range(datetime(2016, 9, 1, 7), datetime(2016, 9, 2,7),
       timedelta(minutes=15))]

pandas_df = pd.DataFrame(index = dts)

res = myfunc(pandas_df.index.values.astype('datetime64[D]'))

print(res)

I get error:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'astype' of type datetime64[]

File "test5.py", line 17:
def myfunc(dts):
    epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
    ^

During: typing of get attribute at C:/Users/PUser/PycharmProjects/pythonProjectTEST/test5.py (17)

File "test5.py", line 17:
def myfunc(dts):
    epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
    ^

How can I make this work

1 Answers

Your problem is likely related to this documented issue with numba.


A first workaround would be to define epoch outside of your jit function:

def myfunc(dts):
    @jit(nopython=True)
    def wrapper(dts, epoch):
        if epoch == dts[0]:
            n = 1
        return epoch
    epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
    return wrapper(dts, epoch)

An alternative, hacky solution that also comes to mind would be to render your dates as strings before feeding them to myfunc :

res = myfunc(np.datetime_as_string(pandas_df.index.values, unit='D'))

and define epoch ='1970-01-01' inside of myfunc.

You can finally add a post-processing step after that to convert your strings back to datetime64 or whatever they need be.

Related