【问题标题】:Pandas: Convert Quarterly Data into Monthly DataPandas:将季度数据转换为月度数据
【发布时间】:2020-08-28 21:18:31
【问题描述】:

我需要将一些季度数据转换为月度数据,以便使用另一个数据集。数据如下所示:

Date Value
1/1/2010 100
4/1/2010 130
7/1/2010 160

我需要做的是估算缺失月份的值,使其看起来像这样:

Date Value
1/1/2010 100
2/1/2010 110
3/1/2010 120
4/1/2010 130
5/1/2010 140
6/1/2010 150
7/1/2010 160

之前找不到很多关于如何执行此操作的问题。只有相反(每月到每季度)。我反过来尝试了其中一种方法,但没有奏效:

pd.PeriodIndex(df.Date, freq='M')

在 Pandas 中执行此操作的最简单方法是什么?

【问题讨论】:

    标签: python pandas datetime time-series


    【解决方案1】:

    你可以使用resample:

    # convert to period
    df['Date'] = pd.to_datetime(df['Date']).dt.to_period('M')
    
    # set Date as index and resample
    df.set_index('Date').resample('M').interpolate()
    

    输出:

             Value
    Date          
    2010-01  100.0
    2010-02  110.0
    2010-03  120.0
    2010-04  130.0
    2010-05  140.0
    2010-06  150.0
    2010-07  160.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-14
      • 2019-12-06
      • 2022-11-10
      • 2021-03-08
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多