How to fix the error: The truth value of an array with more than one element is ambiguous

How to fix the error: The truth value of an array with more than one element is ambiguous

我对此有疑问,我知道代码太长太复杂,但我开始了: 这是我正在使用的数据:

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

然后我尝试 运行 函数 Discount_Factor_2 并且我得到这个错误:

Discount_Factor_2(date,maturity_dtime,curve)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

错误在行 if maturity_date > curve[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]

想想这是干什么的 -

假设 maturity_dtime 是一个看起来像这样的 pd.Series -

2020-04-07
2020-08-07
2023-12-07

现在,如果您执行 maturity_date > curve[nrows-1][0],这将迭代检查 maturity_date 中的每个元素是否大于 curve[nrows-1][0]

因此,这将产生另一个 pandas.Series,可能看起来像这样 -

True
False
True

python 中的 if 语句是个傻瓜,需要一个 bool 值,而您只是通过提供一堆布尔值来混淆它。因此,您需要在最后使用 .all().any()(这些是通常要做的事情),具体取决于您想要什么

检查您在 curve[nrows-1][0]maturity_date 中选择的内容。其中之一必须是具有多个元素的数组,因此不能与单个元素进行比较。