Python / Pandas - DataFrame index - move one month forward
I have a DataFrame:
Actual Pred
Date
2005-04-01 10.2 10.364470
2005-05-01 9.4 9.542778
2005-06-01 9.5 9.684794
2005-07-01 9.4 9.547604
2005-08-01 9.7 9.768893
I want to add one month to each DataFrame index so that it looks like this:
Actual Pred
Date
2005-05-01 10.2 10.364470
2005-06-01 9.4 9.542778
2005-07-01 9.5 9.684794
2005-08-01 9.4 9.547604
2005-09-01 9.7 9.768893
How to do it?
Important comment:
When I command print type(DataFrame.index[0])
to find out the data type of the index, I get:
<class 'pandas.tslib.Timestamp'>
Just to find out that it is a Pandas mark.
+1
source to share
1 answer
You can use pd.DateOffset
:
In [82]: df
Out[82]:
Actual Pred
Date
2005-04-01 10.2 10.364470
2005-05-01 9.4 9.542778
2005-06-01 9.5 9.684794
2005-07-01 9.4 9.547604
2005-08-01 9.7 9.768893
df.index = df.index + pd.DateOffset(months=1)
In [85]: df
Out[85]:
Actual Pred
Date
2005-05-01 10.2 10.364470
2005-06-01 9.4 9.542778
2005-07-01 9.5 9.684794
2005-08-01 9.4 9.547604
2005-09-01 9.7 9.768893
+5
source to share