【问题标题】:cannot convert the series to <class 'float'> Error while using one module使用一个模块时无法将系列转换为 <class 'float'> 错误
【发布时间】:2020-02-24 14:40:08
【问题描述】:

我有这个数据框:

     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 ", 'occured at index 0')

【问题讨论】:

    标签: python-3.x pandas


    【解决方案1】:

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

    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)
    

    以您在代码中实现的方式,您实际上使用数据框中的列 (pd.Series) 调用了 mb.BS。请在此处查看 pandas apply 文档。

    【讨论】:

    • 成功了。你能告诉我为什么我的方式,它抛出一个错误而你的工作?错误的原因是什么?
    • @saurabh 我在答案中添加了评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多