使用 numba 引擎时 pandas 滚动窗口并行化问题

问题描述 投票:0回答:0

我有庞大的数据框,我需要在熊猫中使用滚动窗口来计算斜率。下面的代码在没有 numba 引擎的情况下工作正常,但在使用 numba 引擎时出错。这个问题看起来像是在

numba_func
中完成的检查。知道如何在仍然使用并行计算的同时克服这个错误吗?

def slope(x):
    x = x.values
    length = len(x)
    if length < 2:
       return np.nan
    slope = (x[-1] - x[0])/(length -1)
    return slope

df = pd.DataFrame({"id":[1,1,1,1,1,2,2,2,2,2,2], 'a': [1,3,2,4,5,6,3,5,8,12,30], 'b':range(10,21)})
df.groupby('id', as_index=False).rolling(min_periods=2, window=5).apply(slope)
df.groupby('id', as_index=False).rolling(min_periods=2, window=5).apply(slope, raw = True, engine="numba", engine_kwargs={"parallel": True})

错误:

    1419 else:
   1420     raise ValueError("engine must be either 'numba' or 'cython'")
-> 1422 return self._apply(
...
    
        if len(window) - count_nan >= minimum_periods:
            result[i] = numba_func(window, *args)
            ^
python pandas optimization numba
© www.soinside.com 2019 - 2024. All rights reserved.