Is it possible to create NPDatetime objects within a @jit(nopython=True) function? From what I can tell, initializing these objects requires using string objects which seems to prevent nopython functions from compiling.
Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.
Numba has two compilation modes: nopython mode and object mode. In nopython mode, the Numba compiler will generate code that does not access the Python C API. This mode produces the highest performance code, but requires that the native types of all values in the function can be inferred.
Starting in NumPy 1.7, there are core array data types which natively support datetime functionality. The data type is called datetime64 , so named because datetime is already taken by the Python standard library.
Introduction. For the uninitiated Numba is an open-source JIT compiler that translates a subset of Python/NumPy code into an optimized machine code using the LLVM compiler library. In short Numba makes Python/NumPy code runs faster. It achieves this by compiling your Python code into native machine code.
You can certainly do operations on numpy datetimes.
import numpy as np
import numba as nb
@nb.njit
def diff_dt(a, b):
return a - b
x = np.datetime64('2005-02-25')
y = np.datetime64('2005-02-27')
diff_dt(x, y)
You however can't create a datetime object it appears:
@nb.njit
def create_dt(s):
return np.datetime64(s)
# Fails
create_dt('2005-02-25')
I was facing a similar problem. What I did was just convert my datetimes into integer seconds since the epoch, then after the jit operation, recast them to datetimes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With