Rag*_*rok 2 python datetime time-series pandas
我有一些季度数据需要转换为月度数据以便处理另一个数据集。数据如下所示:
Date Value
1/1/2010 100
4/1/2010 130
7/1/2010 160
Run Code Online (Sandbox Code Playgroud)
我需要做的是估算缺失月份的值,使其看起来像这样:
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
Run Code Online (Sandbox Code Playgroud)
找不到关于如何执行此操作的许多先前问题。只有相反(每月到每季度)。我反向尝试了其中一种方法,但没有奏效:
pd.PeriodIndex(df.Date, freq='M')
Run Code Online (Sandbox Code Playgroud)
在 Pandas 中执行此操作的最简单方法是什么?
您可以使用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()
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1553 次 |
| 最近记录: |