【问题标题】:Python/Pandas - DataFrame Index - Move one month forwardPython/Pandas - 数据帧索引 - 向前移动一个月
【发布时间】:2016-05-07 19:48:14
【问题描述】:

我有一个数据框:

                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 的索引添加一个月,所以它看起来像这样:

                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])找出索引的数据类型时,我得到:

<class 'pandas.tslib.Timestamp'>

只是为了让您知道它是 Pandas 时间戳。

【问题讨论】:

    标签: python datetime pandas timestamp


    【解决方案1】:

    你可以使用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    
    

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2018-06-24
      • 2017-06-30
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多