使用一个模块时出错,python-3.x,pandas"/>

首页 > 解决方案 > 无法将系列转换为使用一个模块时出错

问题描述

我有这个数据框:

     bprice    cprice  strike  irate  tleft
0    26436.90   91.70   26400     10    0.5
1    26423.00   98.90   26400     10    0.5
2    26436.00   90.00   26400     10    0.5
3    26416.35  103.60   26400     10    0.5

我正在应用一个功能(http://code.mibian.net/

c = mibian.BS([1.4565, 1.45, 1, 30], callPrice=0.0359) 在我的数据框上,

df2['IV'] = df2.apply(lambda df: mb.BS([df2['bprice'],26400,10,df2['tleft']],
                                 callPrice=df2['cprice']).impliedVolatility, axis=1)

但我得到了错误

TypeError: ("cannot convert the series to <class 'float'>", 'occured at index 0')

标签: python-3.xpandas

解决方案


我建议你包装函数并做这样的事情 -

def my_func(x):
    return mb.BS([x['bprice'], 26400, 10 x['tleft']],
        callPrice=x['cprice']).impliedVolatility

df2['IV'] = df2.apply(my_func, axis=1)

在您在代码中实现的方式中,您实际上mb.BS使用数据框中的列(pd.Series)进行了调用。请参阅此处的 pandas应用文档。


推荐阅读