python-3.x - 无法将系列转换为使用一个模块时出错
问题描述
我有这个数据框:
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')
解决方案
我建议你包装函数并做这样的事情 -
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应用文档。
推荐阅读
- python - SQLAlchemy 声明式通过辅助属性获取对象
- firebase - 将 UID 与文档字段匹配的 Firestore 规则不起作用
- ios - 电池电量检查背景
- prometheus - Prometheus 节点导出器未报告非 root 拥有的挂载
- keras - 为什么我的keras模型预测的概率总是零
- ansible - Ansible:使用 JMESPath 过滤包含字符串的元素
- sql-server - SQL Server 中浮点计算的结果不一致
- typescript - 如何从打字稿中的对象文字类型中提取一种类型?
- gradle - 当构建任务已经存在时,为模块构建任务附加dependsOn
- hyperledger-fabric - 更新或替换超级账本 fabrc 身份的最佳方法是什么