using lower_cast (or finding some other way to divide datetimes)

0 views
Skip to first unread message

Anthony Morgan

unread,
Aug 30, 2017, 4:09:03 PM8/30/17
to Numba Public Discussion - Public
I'm sure I'm doing something daft here, but perhaps someone could enlighten me as to why this doesn't work:

import numpy as np
from numba import jit
from numba import types
from numba.extending import lower_cast

@lower_cast(types.NPDatetime('ns'), types.int64)
def dt_val(context, builder, fromty, toty, value):
    return value

@jit(nopython=True, locals={'x': types.int64})
def test(x):
    return x // 1000000000

res = test(np.datetime64(60*1000000000,'ns'))

fails with, 

numba.errors.TypingError: Failed at nopython (nopython frontend)
No conversion from datetime64(ns) to int64 for 'x', defined at None

or perhaps any alternative approach for taking the floor of a datetime64, that doesn't involve taking a view of the item outside of numba would be appreciated. 

thanks

Chris Bartak

unread,
Aug 30, 2017, 4:27:00 PM8/30/17
to numba...@continuum.io
It seems like you can directly convert inside your calling function

@jit(nopython=True)
def test(x):
    return np.int64(x) // 1000000000


--
You received this message because you are subscribed to the Google Groups "Numba Public Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numba-users+unsubscribe@continuum.io.
To post to this group, send email to numba...@continuum.io.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/numba-users/28c15510-e239-425e-84bf-6ad0d36d386d%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Anthony Morgan

unread,
Aug 30, 2017, 7:36:56 PM8/30/17
to numba...@continuum.io
That's really great, thank you. It's a bit annoying that the inverse doesn't work, because the datetime64 constructor ins't exposed to numba, but I can live with that easily enough if there isn't an easy solution to that:

import numpy as np
from numba import jit
from numba import types
from numba.extending import lower_cast

@lower_cast(types.NPDatetime('ns'), types.int64)
def dt_val(context, builder, fromty, toty, value):
    return value

@lower_cast(types.int64, types.NPDatetime('ns'))
def val_dt(context, builder, fromty, toty, value):
    return value

@jit(nopython=True)
def test(x):
    y = np.int64(x) // 1000000000
    return np.datetime64(y,'ns')

res = test(np.datetime64(60*1000000000,'ns'))
Reply all
Reply to author
Forward
0 new messages