Python 如何修复错误:包含多个元素的数组的真值不明确
我对此有一个问题,我知道代码太长太复杂,但我想: 这是我正在使用的数据:Python 如何修复错误:包含多个元素的数组的真值不明确,python,pandas,optimization,scipy,minimize,Python,Pandas,Optimization,Scipy,Minimize,我对此有一个问题,我知道代码太长太复杂,但我想: 这是我正在使用的数据: Data: date = dt.datetime(2018, 6, 26) maturity_dtime = DatetimeIndex(['2020-04-07', '2020-08-07', '2020-12-07', '2023-12-07', '2027-12-07', '2032-12-07', '2040-02-07'], dtype='datetime64[ns]'
Data:
date = dt.datetime(2018, 6, 26)
maturity_dtime = DatetimeIndex(['2020-04-07', '2020-08-07', '2020-12-07', '2023-12-07',
'2027-12-07', '2032-12-07', '2040-02-07'],
dtype='datetime64[ns]', freq=None)
curve = ['act/365','Lineal','Anual',
[datetime.datetime(2018, 6, 27, 0, 0), 4.105922851627142e-05],
[datetime.datetime(2018, 7, 26, 0, 0), 0.001200502096981415],
[datetime.datetime(2018, 9, 26, 0, 0), 0.0034882824213942065],
[datetime.datetime(2018, 12, 26, 0, 0), 0.006427227712844319],
[datetime.datetime(2019, 3, 26, 0, 0), 0.008915157135919838],
[datetime.datetime(2019, 6, 26, 0, 0), 0.011097508773927123],
[datetime.datetime(2020, 6, 26, 0, 0), 0.0171882727144943]]
那么我有这个函数:
def day_count(start_date, end_date, basis):
if basis == 'act/365':
days = (end_date - start_date).days
else:
print('fail')
return days
def year_fraction(start_date, end_date, basis):
if basis == "act/365":
yf = day_count(start_date, end_date,basis) / 360.0
else:
print('fail')
return yf
def interpol_curva(date,maturity_date,curve):
base=curve[0]
interpol=curve[1]
#compo_fg=curve[2]
nrows=int(len(curve))
if maturity_date > curve[nrows-1][0]: #Here is the mistake
maturity_date=curve[nrows-1][0]
if maturity_date<curve[3][0]:
maturity_date=curve[3][0]
r1=3
while maturity_date>curve[r1][0] and r1<nrows-1:
r1=r1+1
r1=r1-1
if r1==2:
r1=3
if r1>=nrows-1:
r1=nrows-2
r2=r1+1
#t1=year_fraction_2(date, curve[r1][0], base)
#t2=year_fraction_2(date, curve[r2][0], base)
#tt=year_fraction_2(date, matDate, base)
if base=='act/360' or base=='act/365':
yf1=(maturity_date-curve[r1][0]).days
yf2=(curve[r2][0]-maturity_date).days
yftt=(curve[r2][0]-curve[r1][0]).days
else:
print("fail")
if interpol=='Lineal':
return (curve[r1][1]*yf2+curve[r2][1]*yf1)/yftt
def Discount_Factor_2(value_date,maturity_date,curve):
basis=curve[0]
Composition=curve[2]
yf = year_fraction(value_date, maturity_date, basis)
r=interpol_curva(value_date,maturity_date,curve)
if Composition == "Anual":
df = 1 / (1 + r * yf)
else:
print("fail")
return df
如果到期日>曲线[nrows-1][0]
我想知道是否有办法解决它。稍后,我想通过更改创建变量曲线的参数来最小化该函数的结果。
非常感谢,如果不清楚的话,我很抱歉。谢谢你花时间
编辑:添加完整的回溯:
Discount_Factor_2(value_date, maturity_dtime, curve)
Traceback (most recent call last):
File "<ipython-input-30-9cbb6735a3e3>", line 1, in <module>
Discount_Factor_2(value_date, maturity_dtime, curve)
File "<ipython-input-20-181d050d0cd4>", line 251, in Discount_Factor_2
r=interpol_curva(value_date,maturity_date,curve)
File "<ipython-input-20-181d050d0cd4>", line 178, in interpol_curva
if maturity_date > curve[nrows-1][0]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
问题在于您的if声明中-
if maturity_date > curve[nrows-1][0]
想想这是怎么回事-
假设成熟时间是一个pd系列,看起来像这样-
2020-04-07
2020-08-07
2023-12-07
True
False
True
现在,如果您执行到期日>曲线[nrows-1][0],这将迭代检查到期日中的每个元素是否大于曲线[nrows-1][0]
因此,这将产生另一只熊猫。系列可能看起来像这样-
2020-04-07
2020-08-07
2023-12-07
True
False
True
python中的if语句是simpleton,它需要一个bool值,而您只需提供一组布尔值就可以将其混淆。因此,您需要使用.all或.any,这是最后通常要做的事情,具体取决于您想要什么问题在于您的if语句-
if maturity_date > curve[nrows-1][0]
想想这是怎么回事-
假设成熟时间是一个pd系列,看起来像这样-
2020-04-07
2020-08-07
2023-12-07
True
False
True
现在,如果您执行到期日>曲线[nrows-1][0],这将迭代检查到期日中的每个元素是否大于曲线[nrows-1][0]
因此,这将产生另一只熊猫。系列可能看起来像这样-
2020-04-07
2020-08-07
2023-12-07
True
False
True
python中的if语句是simpleton,它需要一个bool值,而您只需提供一组布尔值就可以将其混淆。因此,您需要使用.all或.any,这是最后通常要做的事情,具体取决于您想要什么检查您在曲线[nrows-1][0]和到期日中选择了什么。其中一个必须是包含多个元素的数组,因此无法与单个元素进行比较。检查您在曲线[nrows-1][0]和到期日中选择的内容。其中一个必须是包含多个元素的数组,因此无法与单个元素进行比较。您还可以显示异常堆栈跟踪吗?@MohitMotwani我编辑它添加回溯,我不知道您的意思。您还可以显示异常堆栈跟踪吗?@MohitMotwani我编辑它添加回溯,我不知道你是不是那个意思,我该写在哪里。any@Amartin在你的pd.Series if到期日>曲线[nrows-1][0]之上。我应该写的任何地方。any@Amartin如果到期日>曲线[nrows-1][0],则在pd.系列的顶部。任何