gpt4 book ai didi

Python 多处理类型错误

转载 作者:行者123 更新时间:2023-11-30 23:08:13 26 4
gpt4 key购买 nike

我有一些这样的代码。

当我执行 jobs[i].get() 时遇到错误,这是一个 multiprocessing.pool.ApplyResult 对象

类型错误:“系列”对象不可调用

import multiprocessing 
import numpy as np
import pandas as pd
def tf(x):
return np.mean(x)

def main():
pool=multiprocessing.Pool(6)
sds=pd.Series(np.random.normal(0,.01,1000))
jobs=[]

for i in xrange(10):
jobs.append(pool.apply_async(pd.rolling_apply(sds,2,tf)))

pool.close()
pool.join()


for i in xrange(len(jobs)):
jobs[i].get()




if __name__=="__main__":
main()

最佳答案

TypeError: 'Series' object is not callable

是因为您将函数的结果传递给池,而不是函数本身,正如您应该做的那样。

您应该执行以下操作:

import multiprocessing 
import numpy as np
import pandas as pd

def tf(x):
return np.mean(x)

def main():
pool=multiprocessing.Pool(6)
sds=pd.Series(np.random.normal(0,.01,1000))
jobs=[]

for i in xrange(10):
jobs.append( pool.apply_async(pd.rolling_apply, (sds,2,tf)) )

pool.close()
pool.join()


for i in xrange(len(jobs)):
jobs[i].get()

if __name__=="__main__":
main()

输出:

0          NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64
0 NaN
1 0.009682
2 0.002747
3 0.005108
4 0.002115
5 0.000449
6 0.011551
7 0.000686
8 -0.004860
9 -0.007568
10 -0.005052
11 -0.004860
12 -0.003354
13 0.005291
14 0.000845
...
985 -0.001762
986 -0.008001
987 -0.007872
988 -0.007356
989 -0.003436
990 -0.003725
991 -0.010541
992 -0.001246
993 0.002308
994 0.004322
995 0.010862
996 0.003545
997 -0.002039
998 0.003992
999 -0.006216
Length: 1000, dtype: float64

关于Python 多处理类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31817434/

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