Python/Pandas-DataFrame索引-向前移动一个月 [英] Python/Pandas - DataFrame Index - Move one month forward
本文介绍了Python/Pandas-DataFrame索引-向前移动一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataFrame:
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
我想为每个DataFrame的索引添加一个月,所以它看起来像这样:
I want to add one month to each of the DataFrame's indexes, so it gets to look 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
我该怎么做?
重要评论:
当我命令print type(DataFrame.index[0])
找出索引的数据类型时,我得到:
When I command print type(DataFrame.index[0])
to find out the data type of the index, I get:
<class 'pandas.tslib.Timestamp'>
只是让您知道这是熊猫时间戳.
Just for you to know that it is a Pandas Timestamp.
推荐答案
您可以使用 pd.DateOffset
:
You could 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
这篇关于Python/Pandas-DataFrame索引-向前移动一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文