0

I have the following code. However i am having error when executing same. I want the function to calculate the schedule date and ouput same to a new dataframe. It seems the error is in the for loop. I am new to this so just trying to figure out

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)

Desired Output

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Avishen
  • 19
  • 5

1 Answers1

0

I assume what you are wanting to do is to modify a dataframe row by row (if there is more to it there may be a better solution) :

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

EDIT : You just want to iterate through the rows :

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

There's other ways to append to a dataframe row by row but this way is also fine.

jeed
  • 136
  • 5