3

I would like to create an array of dates in a Numba function, running in nopython mode.

I can't see a date type, so I am trying NPDatetime.

My attempted code is:

import numba as nb
import numpy as np


@nb.jit(nopython=True)
def xxx():
    return np.empty(10, dtype=nb.types.NPDatetime('D'))


print(xxx())

However, the code returns this error:

Unknown attribute 'NPDatetime' of type Module(<module 'numba.types' from '/home/xxx/anaconda3/lib/python3.6/site-packages/numba/types/__init__.py'>)

My numba version is 0.39.0

2 Answers 2

5
+25

Edit: I have to correct myself, both numpy.empty() and datetime64 are supported by numba:

Numba supports the following Numpy scalar types: [...] Datetimes and timestamps: of any unit [...]
Source: Section 2.7.1

numpy.empty() (only the 2 first arguments)
Source: Section 2.7.3.3

No idea what causes your problem.


You have specified the dtype argument incorrectly. Numpy doesn't use the numba classes for it. Here is how you could specify dtype correctly: (Read more here and here)

dtype="datetime64[D]"

But even if you specified the argument that way, it would not work. The nopython argument to @nb.jit() doesn't natively support that type. Here is the corrected code: (Read more here)

import numba as nb
import numpy as np

@nb.jit
def xxx():
  return np.empty(10, dtype="datetime64[D]")

print(xxx())

But consider giving numba a type hint:

import numba as nb
import numpy as np

@nb.jit(nb.types.NPDatetime('D')()) # returns datatime, no arguments
def xxx():
  return np.empty(10, dtype="datetime64[D]")

print(xxx())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion. Is it currently impossible to use a date type in nopython mode? At present, I am getting around it by using integers instead.
2

I have never used numba or numpy before so it took a little bit of research, but this should work.

import numba as nb
import numpy as np
from numba import *

def xxx():
    return np.empty(10, dtype=np.dtype(np.datetime64('2014')))

jitcompute = nb.jit(nopython=True)(xxx)
print(xxx())

let me know if this works!

Edit:

as stated in the comments my code was executed without the just in time decorator implemented, with future research it seems like there is a bug where dtype cant be used inside a JIT decorated function https://github.com/numba/numba/issues/3066

I have spent 4-5 hours on this problem now and i cannot find a way around it, you can parse the arrays created through the xxx function, but you are not able to use empty() or zeros() inside the decorated function

1 Comment

Thanks @asynts stupid mistake by me, will take a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.