pandas :将季度数据转换为每月数据 [英] Pandas: Convert Quarterly Data into Monthly Data

查看:116
本文介绍了 pandas :将季度数据转换为每月数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some quarterly data that I need to convert to monthly in order to work with another data set. The data looks like this:

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

我需要做的就是估算值对于缺少的月份,看起来像这样:

What I need to do is impute the values for the missing months so that it looks like this:

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

找不到关于如何执行此操作的先前问题。仅相反(每月到每季度)。我反向尝试了其中一种方法,但是没有用:

Couldn't find many previous questions on how to do this. Only the reverse (monthly to quarterly). I tried one of those methodologies in reverse, but it didn't work:

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

在Pandas中最简单的方法是什么?

What would be the easiest way to go about doing this in Pandas?

推荐答案

您可以使用重新采样

# 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

这篇关于 pandas :将季度数据转换为每月数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆