Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numpy.datetime in numba nopython functions?

Tags:

numpy

numba

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.

like image 228
triphook Avatar asked Feb 03 '17 18:02

triphook


People also ask

Does Numba work with NumPy?

Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.

What is Nopython in Numba?

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.

Does NumPy have datetime?

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.

Does Numba make NumPy faster?

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.


2 Answers

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')
like image 113
JoshAdel Avatar answered Dec 18 '22 14:12

JoshAdel


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.

like image 43
Corey Levinson Avatar answered Dec 18 '22 14:12

Corey Levinson