0

Dear everyone and beloved community,

  • I'm really being challenged with a piece of code, and would hihgly appreciate some guidance on the interpretation. The main question to the code is: How can the variable "days" in the below code, return a number of days?

To my understanding, it will return a time stamp, such as: 14/11/2022 00:00. Additionally, notice in the end that the maturity date of the bond is added: ([row.maturity]). So, that would seem it would make an addition of to time-stamps, which does not make much sense to me.


The code:

cash_flows = {}
for _, row in bond_df.iterrows():
    
    cash_flow_df = pd.DataFrame(columns=['Date', 'Cashflow'])
    
    days = [row.price_date] + [row.next_coupon_date.replace(year=i + row.next_coupon_date.year) for i in range(row.maturity.year - row.next_coupon_date.year)] + [row.maturity]
    cash_flow = [-row.dirty_price] + [row.coupon for _ in range(row.maturity.year - row.next_coupon_date.year)] + [100 + row.coupon]
    
    cash_flow_df['Date'] = days     
    cash_flow_df['Cashflow'] = cash_flow
    
    cash_flows[row.bond] = cash_flow_df

The dataframe, bond_df:

The dataframe (with name: bond_df) is a 12x8 pandas dataframe: Please see below. bond_df

Interpretation - With the above piece of code, I am constructing...:

  • an empty dictionary to contain all cashflows for each bond in my df computed in the loop.
  • a loop iterating an arbitrary numer of times (using '_'), and saving the iteration in the variable 'row', and using iterrows to iterate through my df.
  • a pandas dataframe to save the current computed cashflow in the loop, updating its values in each iteration.
  • ! PROBLEM:
    calculating the numder of days left, until the next coupon date. This piece of code iterates through each row (bond). For the first bond, it selects the date when the price was observed for the bond. Then, it adds the date for the next expected coupon (it replaces the year in that date, with the next year value, for a range between the next expected coupon, and maturity). Finally, it adds the date of maturity for each row.
    This is the part that I am being troubled with.

Output (the correct result): cash_flows

I hope there is someone more experienced, who can share some valuable insights.

All the best, Nic

FObersteiner
  • 22,500
  • 8
  • 42
  • 72

0 Answers0