gpt4 book ai didi

python - 在 pandas 的 rolling().apply() 中,args 在 enigne ="numba"时被缓存

转载 作者:行者123 更新时间:2023-12-05 05:39:46 24 4
gpt4 key购买 nike

在pandas的rolling().apply()中,enigne="numba"时会缓存Args。
这是正确的行为吗?
有什么办法可以防止 args 被缓存吗?

示例代码如下

import pandas as pd
import numba as nb

@nb.jit
def test_func(x, c):
return c

@nb.jit
def test_func2(x, c):
return c

s = pd.Series(range(5))
print("s")
print(s)

rolling_s1 = s.rolling(window=3).apply(test_func, raw=True, engine="numba", args=(100,))
print("rolling_s1")
display(rolling_s1)

rolling_s2 = s.rolling(window=3).apply(test_func, raw=True, engine="numba", args=(-100,))
print("rolling_s2")
display(rolling_s2)

rolling_s3 = s.rolling(window=3).apply(test_func2, raw=True, engine="numba", args=(-100,))
print("rolling_s3")
display(rolling_s3)

print("test_func(None, 200)")
print(test_func(None, 200))

print("test_func2(None, -200)")
print(test_func2(None, -200))

执行结果如下

s
0 0
1 1
2 2
3 3
4 4
dtype: int64
rolling_s1
0 NaN
1 NaN
2 100.0
3 100.0
4 100.0
dtype: float64
rolling_s2
0 NaN
1 NaN
2 100.0
3 100.0
4 100.0
dtype: float64
rolling_s3
0 NaN
1 NaN
2 -100.0
3 -100.0
4 -100.0
dtype: float64
test_func(None, 200)
200
test_func2(None, -200)
-200

rollig_s2的值应该是-100,结果是100。

最佳答案

事实证明这是一个错误并且它已被在版本 1.4.0 中解决。参见 this closed bug .

关于python - 在 pandas 的 rolling().apply() 中,args 在 enigne ="numba"时被缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72593721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com