1

I have a dataframe where I am trying to add a predefined integer value to a datetime.

id  start date term

ab  13-09-2017   6

What I would like to do is add the integer term 6 as years to the datetime.

Any advice would be appreciated.

Many thanks

Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • 2
    Quick search https://stackoverflow.com/questions/48796729/how-to-add-a-year-to-a-column-of-dates-in-pandas – Adirmola Mar 31 '19 at 14:58

1 Answers1

2

Use pd.DateOffset :

df = pd.DataFrame({'id':['ab'], 'startdate':['13-09-2017'], 'term':[6]})
df['startdate'] = pd.to_datetime(df['startdate'])

df['startdate'] = df.apply(lambda x: x['startdate'] + pd.DateOffset(years=int(x['term'])), axis=1)

print(df)

Output:

cccid startdate  term                                                                                                                         
0  ab 2023-09-13     6 
Sociopath
  • 13,068
  • 19
  • 47
  • 75