gpt4 book ai didi

python - ExcelPython返回类型不匹配问题

转载 作者:行者123 更新时间:2023-12-01 05:18:57 35 4
gpt4 key购买 nike

所以我使用 VBA 和 ExcelPython 引用从 Excel 文件调用 python 脚本。 python 脚本工作正常,除了 Excel 不断告诉我在指定的行上有类型不匹配:

Function calcCapValue(times As Range, fRates As Range, strike As Range, vol As Range, delta As Double, pv As Range) As Variant

Set methods = PyModule("PyFunctions", AddPath:=ThisWorkbook.Path)
Set result = PyCall(methods, "CalculateCapValue", KwArgs:=PyDict("times", times.Value2, "fwdRates", fRates.Value2, "strike", strike.Cells(1, 1).Value2, "flatVol", vol.Cells(1, 1).Value2, "delta", delta, "pv", pv.Cells(1, 1).Value2))

calcCapValue = PyVar(PyGetItem(result, "res")) ' <--- TYPE MISMATCH HERE

Exit Function

End Function

不明白为什么,我正在遵循这里的示例代码:https://sourceforge.net/p/excelpython/wiki/Putting%20it%20all%20together/在这里:http://www.codeproject.com/Articles/639887/Calling-Python-code-from-Excel-with-ExcelPython

仍然出现这种类型不匹配的情况,我不明白为什么。

这是Python脚本:

#imports
import numpy as np
from scipy.stats import norm

#
# Cap Price Calculation
#
def CalculateCapValue(times, fwdRates, strike, flatVol, delta, pv):

capPrice = 0;


#Iterate through each time for caplet price

for i in range(0, len(times)):

ifr = float(fwdRates[i][0])
iti = float(times[i][0])

d1 = (np.log(ifr/strike)+((flatVol**2)*iti)/2)/(flatVol*np.sqrt(iti))
d2 = d1 - (flatVol*np.sqrt(iti))
Nd1 = norm.cdf(d1)
Nd2 = norm.cdf(d2)

capPrice += pv*delta*(ifr*Nd1 - strike*Nd2)

return {"res": capPrice}

谢谢!

最佳答案

Sourceforge 上的 ExcelPython 讨论论坛回答的问题:

http://sourceforge.net/p/excelpython/discussion/general/thread/97f6c1b0/

关于python - ExcelPython返回类型不匹配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752924/

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