【问题标题】:How to fix the error: The truth value of an array with more than one element is ambiguous如何修复错误:具有多个元素的数组的真值不明确
【发布时间】:2019-04-16 04:22:02
【问题描述】:

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

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()

错误在maturity_date &gt; 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()

【问题讨论】:

  • 您能否也显示您的异常堆栈跟踪?
  • @MohitMotwani 我编辑它添加了回溯,我不知道你是不是这个意思。

标签: python pandas optimization scipy minimize


【解决方案1】:

问题出在您的if 声明中 -

if maturity_date > curve[nrows-1][0]

想想这是做什么的 -

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

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

现在如果你做maturity_date &gt; curve[nrows-1][0],这将反复检查maturity_date中的每个元素是否大于curve[nrows-1][0]

所以,这将产生另一个pandas.Series,它可能看起来像这样 -

True
False
True

python 中的 if 语句是简单的,它需要一个 bool 值,而您只是通过提供一堆布尔值来混淆它。所以,你需要在最后使用.all().any()(这些是通常的待办事项),这取决于你想要什么

【讨论】:

  • 我应该在哪里写 .any()
  • @Amartin 在你的pd.Series if (maturity_date &gt; curve[nrows-1][0]).any()
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 2019-08-21
    • 1970-01-01
    • 2019-03-19
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多