我的Dataframe看起来像这样:
In [325]: TYVOL.tail()
Out[325]:
Close
Date
2017-11-24 0.027705
2017-11-27 0.029335
2017-11-28 0.029335
2017-11-29 0.029498
2017-11-30 0.031454
尝试了这个:
TYVOL['pb'] = [my_probability_gamma(TYVOL.Close[date],shape0,shape1,shape2,scale0,
scale1,scale2,pbv0,pbv1,pbv2,date) for date in TYVOL.index]
抛出KeyError:Timestamp
我做错了什么明显的?谢谢你的帮助。
答案 0 :(得分:2)
我认为你需要loc
:
TYVOL['pb'] = [my_probability_gamma(TYVOL.loc[date, 'Close'],shape0,shape1,shape2,scale0,
scale1,scale2,pbv0,pbv1,pbv2,date) for date in TYVOL.index]
或apply
而不是date
使用x.name
:
f = lambda x: my_probability_gamma(x['Close'],shape0,shape1,shape2,
scale0, scale1,scale2,pbv0,pbv1,pbv2,x.name)
TYVOL['pb'] = TYVOL.apply(f, axis=1)
或使用iteritems
:
TYVOL['pb'] = [my_probability_gamma(c,shape0,shape1,shape2,scale0,
scale1,scale2,pbv0,pbv1,pbv2,d) for d, c in TYVOL['Close'].iteritems()]
测试:
def my_probability_gamma(x,y,z):
return (x,y,z)
shape0 = 1
TYVOL['pb'] = [my_probability_gamma(TYVOL.loc[date, 'Close'],shape0,date) for date in TYVOL.index]
print (TYVOL)
Close pb
Date
2017-11-24 0.027705 (0.027705, 1, 2017-11-24 00:00:00)
2017-11-27 0.029335 (0.029335, 1, 2017-11-27 00:00:00)
2017-11-28 0.029335 (0.029335, 1, 2017-11-28 00:00:00)
2017-11-29 0.029498 (0.029498, 1, 2017-11-29 00:00:00)
2017-11-30 0.031454 (0.031454, 1, 2017-11-30 00:00:00)
shape0 = 1
f = lambda x: my_probability_gamma(x['Close'],shape0,x.name)
TYVOL['pb'] = TYVOL.apply(f, axis=1)
print (TYVOL)
Close pb
Date
2017-11-24 0.027705 (0.027705, 1, 2017-11-24T00:00:00.000000000)
2017-11-27 0.029335 (0.029335, 1, 2017-11-27T00:00:00.000000000)
2017-11-28 0.029335 (0.029335, 1, 2017-11-28T00:00:00.000000000)
2017-11-29 0.029498 (0.029498, 1, 2017-11-29T00:00:00.000000000)
2017-11-30 0.031454 (0.031454, 1, 2017-11-30T00:00:00.000000000)
TYVOL['pb'] = [my_probability_gamma(c,shape0,d) for d, c in TYVOL['Close'].iteritems()]
print (TYVOL)
Close pb
Date
2017-11-24 0.027705 (0.027705, 1, 2017-11-24 00:00:00)
2017-11-27 0.029335 (0.029335000000000003, 1, 2017-11-27 00:00:00)
2017-11-28 0.029335 (0.029335000000000003, 1, 2017-11-28 00:00:00)
2017-11-29 0.029498 (0.029498000000000003, 1, 2017-11-29 00:00:00)
2017-11-30 0.031454 (0.031454, 1, 2017-11-30 00:00:00)