python - 如何修复错误 : The truth value of an array with more than one element is ambiguous

标签 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]', 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 值,而你只是通过提供一堆 bool 值来混淆它。因此,您需要在最后使用 .all().any() (这些是通常要做的事情),具体取决于您想要什么

关于python - 如何修复错误 : The truth value of an array with more than one element is ambiguous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53279238/

相关文章:

python - Pandas :逻辑和两个数据框

java - 在 Java 中使用 "sincos"

python pandas 交叉表数据透视表(按同一类别)

python - 如何在pandas中groupby之后获得两组之间的p值?

python - Pandas - 添加列,匹配索引

mysql - 针对表 4.000.000 条记录优化 Mysql

c# - 这个 "move declaration closer to usage"真的更可取吗?

Python-twitter api.VerifyCredentials() 返回 none

python - 为什么 < 不能从命令行(简单的 Python 代码)读取文件?

python - PyQt4 : Window shows up at another position after hide() and show()