ValueError: too many values to unpack Pandas
ValueError: too many values to unpack Pandas
我有以下代码。但是我在执行相同时遇到错误。我希望函数计算计划日期并将其输出到新的数据框。
看来错误在 for 循环中。我对此很陌生,所以只是想弄清楚
import QuantLib as ql
import pandas as pd
import numpy as np
import datetime
from dateutil.parser import parse
data = pd.read_excel (r'C:\Users\Avishen\Desktop\Python\BONDDATA.xlsx')
for ISIN,issuedate,maturitydate in data:
issueDate = ql.Date(issuedate, '%d-%m-%Y')
maturityDate = ql.Date(maturitydate, '%d-%m-%Y')
tenor = ql.Period(ql.Semiannual)
day_count = ql.Thirty360
calendar = ql.UnitedStates()
businessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
# Dates in Bond Period
df['Test']= df['t'].apply(ISIN)
df['Test']= df['t1'].apply(ql.Schedule (issueDate, maturityDate, tenor, calendar, businessConvention,businessConvention , dateGeneration, monthEnd))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-b6dc0a0bd926> in <module>
8
9
---> 10 for ISIN,issuedate,maturitydate in data:
11 issueDate = ql.Date(issuedate, '%d-%m-%Y')
12 maturityDate = ql.Date(maturitydate, '%d-%m-%Y')
ValueError: too many values to unpack (expected 3)
期望的输出
t t1
Test1 Date(22,10,2019)
Test1 Date(8,2,2020)
Test1 Date(8,8,2020)
Test1 Date(8,2,2021)
Test1 Date(8,8,2021)
Test1 Date(8,2,2022)
Test1 Date(8,8,2022)
Test1 Date(8,2,2023)
Test1 Date(8,8,2023)
Test2 Date(31,7,2020)
Test2 Date(21,9,2020)
Test2 Date(21,3,2021)
Test2 Date(21,9,2021)
Test2 Date(21,3,2022)
Test2 Date(21,9,2022)
Test2 Date(21,3,2023)
Test2 Date(21,9,2023)
Test2 Date(21,3,2024)
Test2 Date(21,9,2024)
Test2 Date(21,3,2025)
我假设你想要做的是逐行修改数据框(如果有更多的可能有更好的解决方案):
df['new_issueDate'] = df['issuedate'].apply(lambda row: ql.Date(row, '%d-%m-%Y')
df['new_maturityDate'] = df['maturityDate'].apply(lambda row: ql.Date(row, '%d-%m-%Y')
编辑:您只想遍历行:
t_list = []
t1_list = []
for _, row in data.iterrows():
issueDate = ql.Date(row['issuedate'], '%d-%m-%Y')
maturityDate = ql.Date(row['maturitydate'], '%d-%m-%Y')
tenor = ql.Period(ql.Semiannual)
day_count = ql.Thirty360
calendar = ql.UnitedStates()
businessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
t_list.append(row['ISIN'])
t1_list.append(ql.Schedule(issueDate, maturityDate, tenor, calendar, businessConvention, businessConvention, dateGeneration, monthEnd))
new_df = pd.DataFrame({'t':t_list, 't1':t1_list})
还有其他方法可以逐行附加到数据框,但这种方法也可以。
我有以下代码。但是我在执行相同时遇到错误。我希望函数计算计划日期并将其输出到新的数据框。 看来错误在 for 循环中。我对此很陌生,所以只是想弄清楚
import QuantLib as ql
import pandas as pd
import numpy as np
import datetime
from dateutil.parser import parse
data = pd.read_excel (r'C:\Users\Avishen\Desktop\Python\BONDDATA.xlsx')
for ISIN,issuedate,maturitydate in data:
issueDate = ql.Date(issuedate, '%d-%m-%Y')
maturityDate = ql.Date(maturitydate, '%d-%m-%Y')
tenor = ql.Period(ql.Semiannual)
day_count = ql.Thirty360
calendar = ql.UnitedStates()
businessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
# Dates in Bond Period
df['Test']= df['t'].apply(ISIN)
df['Test']= df['t1'].apply(ql.Schedule (issueDate, maturityDate, tenor, calendar, businessConvention,businessConvention , dateGeneration, monthEnd))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-b6dc0a0bd926> in <module>
8
9
---> 10 for ISIN,issuedate,maturitydate in data:
11 issueDate = ql.Date(issuedate, '%d-%m-%Y')
12 maturityDate = ql.Date(maturitydate, '%d-%m-%Y')
ValueError: too many values to unpack (expected 3)
期望的输出
t t1
Test1 Date(22,10,2019)
Test1 Date(8,2,2020)
Test1 Date(8,8,2020)
Test1 Date(8,2,2021)
Test1 Date(8,8,2021)
Test1 Date(8,2,2022)
Test1 Date(8,8,2022)
Test1 Date(8,2,2023)
Test1 Date(8,8,2023)
Test2 Date(31,7,2020)
Test2 Date(21,9,2020)
Test2 Date(21,3,2021)
Test2 Date(21,9,2021)
Test2 Date(21,3,2022)
Test2 Date(21,9,2022)
Test2 Date(21,3,2023)
Test2 Date(21,9,2023)
Test2 Date(21,3,2024)
Test2 Date(21,9,2024)
Test2 Date(21,3,2025)
我假设你想要做的是逐行修改数据框(如果有更多的可能有更好的解决方案):
df['new_issueDate'] = df['issuedate'].apply(lambda row: ql.Date(row, '%d-%m-%Y')
df['new_maturityDate'] = df['maturityDate'].apply(lambda row: ql.Date(row, '%d-%m-%Y')
编辑:您只想遍历行:
t_list = []
t1_list = []
for _, row in data.iterrows():
issueDate = ql.Date(row['issuedate'], '%d-%m-%Y')
maturityDate = ql.Date(row['maturitydate'], '%d-%m-%Y')
tenor = ql.Period(ql.Semiannual)
day_count = ql.Thirty360
calendar = ql.UnitedStates()
businessConvention = ql.Unadjusted
dateGeneration = ql.DateGeneration.Backward
monthEnd = False
t_list.append(row['ISIN'])
t1_list.append(ql.Schedule(issueDate, maturityDate, tenor, calendar, businessConvention, businessConvention, dateGeneration, monthEnd))
new_df = pd.DataFrame({'t':t_list, 't1':t1_list})
还有其他方法可以逐行附加到数据框,但这种方法也可以。