Reputation: 1947
Let's consider the following code:
import pandas as pd
idx = pd.DatetimeIndex(["2019-07-15", "2019-08-15", "2019-09-15", "2019-10-15"])
DatetimeIndex(['2019-07-15', '2019-08-15', '2019-09-15', '2019-10-15'], dtype='datetime64[ns]', freq=None)
I would like to add one month to each date, but without changing its day.
In other words, the desired output is:
DatetimeIndex(['2019-08-15', '2019-09-15', '2019-10-15', '2019-11-15'], dtype='datetime64[ns]', freq=None)
Do you have any idea how it can be done?
Upvotes: 0
Views: 51
Reputation: 260410
Use pandas.DateOffset
:
out = idx + pd.DateOffset(months=1)
output:
DatetimeIndex(['2019-08-15', '2019-09-15', '2019-10-15', '2019-11-15'],
dtype='datetime64[ns]', freq=None)
NB. be careful to use months=1
and not month=1
(without s
), which would set all months to 01 (January).
Upvotes: 3