在 numba nopython 函数中使用 numpy.datetime?

tri*_*ook 5 numpy numba

是否可以在 @jit(nopython=True) 函数中创建 NPDatetime 对象?据我所知,初始化这些对象需要使用字符串对象,这似乎阻止了 nopython 函数的编译。

Jos*_*del 5

您当然可以对 numpy 日期时间进行操作。

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)
Run Code Online (Sandbox Code Playgroud)

但是,您无法创建它出现的日期时间对象:

@nb.njit
def create_dt(s):
    return np.datetime64(s)

# Fails
create_dt('2005-02-25')
Run Code Online (Sandbox Code Playgroud)