How can I get pandas Timestamp offset by certain amount of months?

Apogentus picture Apogentus · Jun 24, 2015 · Viewed 9.7k times · Source

Suppose I have a pandas Timestamp object t1.

import pandas a pd
t1=pd.Timestamp('2013-04-01 00:00:00')

How can I get another pandas timestamp, offset by k months from t1?

Answer

EdChum picture EdChum · Jun 24, 2015

You can use relativedelta:

In [135]:
k=2
t1 + pd.datetools.relativedelta(months=k)

Out[135]:
Timestamp('2013-06-01 00:00:00')

Or DateOffset:

In [136]:
k=2
t1 + pd.DateOffset(months=k)

Out[136]:
Timestamp('2013-06-01 00:00:00')

Thanks to @AlexRiley for the suggested edit, relativedelta has been moved to

pd.offsets.relativedelta since 0.20.0