How can I get pandas Timestamp offset by certain a

2020-02-13 09:55发布

问题:

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?

回答1:

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