0

I am new to python and have a few questions regarding dates.

Here is an example - I have a list of dates going from 01/01/2012 - 01/01/2025 with a monthly frequency. They also will change based on the data frame. Say one column of dates will have 130 months in between, the other will have 140 months, and so on.

The end goal is: regardless of how many months each set has, I need each "group" to have 180 months. So, in the above example of 01/01/2012 - 1/1/2025, I would need to add enough months to reach 1/1/2027.

Please let me know if this makes sense.

taylorSeries
  • 505
  • 2
  • 6
  • 18
ARE
  • 99
  • 9

1 Answers1

1

So if I understand you correctly, you have some data like:

import pandas as pd, numpy as np
from datetime import date
from dateutil.relativedelta import relativedelta
from random import randint
starts = [dt for i in range(130) if (dt := date(2012, 1, 1) + relativedelta(months=i)) <= date(2020, 1, 1)]
ends = [dt + relativedelta(months=randint(1, 5)) for dt in starts]
df = pd.DataFrame({ 'start': starts, 'end': ends })

so the current duration in months is:

df['duration'] = ((df.end - df.start)/np.timedelta64(1, 'M')).round().astype(int)

and you want to know how many to add to make the duration 180 months?

df['need_to_add'] = 180 - df.duration

then you can calculate a new end by something like:

def add_months(start_date, delta_period):
    end_date = start_date + relativedelta(months=delta_period)
    return end_date
df['new_end'] = df.apply(lambda r: add_months(r['end'], r['need_to_add']), axis=1)

I'm sure I haven't quite understood, as you could just add 180 months to the start date, but hopefully this gets you close to where you need to be.

John M.
  • 775
  • 4
  • 16
  • Sorry maybe I can try to clear it up, so I have a data frame that has a list of dates for futures contracts some ranging from 01-01-2012 to 01-01-2020...etc. However, the end goal is for the length of the maturity dates to be 180 months for all ranges(eg what is the length of months between 01-01-2012 to 01-01-2020 and how many do we need to add to get the total to be 180). So right now if you were to look at all the lengths of the maturity dates in the df you may see a list of numbers such as 55,100,63,81 etc. – ARE Aug 04 '22 at 11:04
  • So I have tons of date range that start and end at different points and essentially I need to write a loop that goes through each series and adds the appropriate amount of months so that the total of each adds up to 180 – ARE Aug 04 '22 at 11:05