我要把日期传递给numba函数。
将它们作为.astype('datetime64D')传入,效果很好。但我也需要在函数内部创建一个划时代的日期。
import numpy as np
import pandas as pd
import numba
from numba import jit
from datetime import datetime, timedelta
def datetime_range(start, end, delta):
current = start
while current < end:
yield current
current += delta
@jit(nopython=True)
def myfunc(dts):
epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
if epoch == dts[0]:
n = 1
return epoch
dts = [dt for dt in
datetime_range(datetime(2016, 9, 1, 7), datetime(2016, 9, 2,7),
timedelta(minutes=15))]
pandas_df = pd.DataFrame(index = dts)
res = myfunc(pandas_df.index.values.astype('datetime64[D]'))
print(res)我得到了错误:
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'astype' of type datetime64[]
File "test5.py", line 17:
def myfunc(dts):
epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
^
During: typing of get attribute at C:/Users/PUser/PycharmProjects/pythonProjectTEST/test5.py (17)
File "test5.py", line 17:
def myfunc(dts):
epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
^我怎样才能让这件事成功
发布于 2021-06-08 20:10:18
您的问题可能与这个有记载的问题和numba有关。
第一个解决办法是在epoch函数之外定义jit:
def myfunc(dts):
@jit(nopython=True)
def wrapper(dts, epoch):
if epoch == dts[0]:
n = 1
return epoch
epoch = np.datetime64('1970-01-01').astype('datetime64[D]')
return wrapper(dts, epoch)另一种也浮现在脑海中的、令人讨厌的解决方案是,在将日期提供给myfunc之前,将日期呈现为字符串:
res = myfunc(np.datetime_as_string(pandas_df.index.values, unit='D'))并在epoch ='1970-01-01'中定义myfunc。
最后,您可以添加一个后处理步骤,以便将您的字符串转换回datetime64或它们所需的任何内容。
https://stackoverflow.com/questions/67736610
复制相似问题