繁体   English   中英

向上采样(分解)季度数据汇总到月度数据

[英]Upsampling (disaggregating) summed quarterly data to monthly data

我正在尝试将数据从季度汇总到每月一次向上采样,但是下面的代码所产生的数字并不是我所需要的。 我需要将这些数据点分解为每月的数字(这些数字加起来等于下一个季度)。 因此,每个新值都需要约为下一个季度的三分之一。

i = ['2000-01-01','2000-04-01','2000-07-01','2000-10-01','2001-01-01','2001-04-01','2001-07-01','2001-10-01']
d = [0,54957.84767,0,0,0,56285.54879,0,0]

df = pd.DataFrame(index=i, data=d)
df.index = pd.to_datetime(df.index,infer_datetime_format=True)
df.index = df.index.to_period('Q')

df.resample('M').first().interpolate(method='cubic')

更新:假设一个玩具系列是[0,0,9]。 所以一月,二月,三月。 3月底的值为9。我希望插值结果为[3,3,3]。 因此,每个月的值为3,当您将它们汇总回四分之一时,它的值为9。

您可以执行此操作,但是由于NaN导致method=cubic无法正常工作。

df.resample('M').asfreq().interpolate()

输出:

                    0
2000-01      0.000000
2000-02  18319.282557
2000-03  36638.565113
2000-04  54957.847670
2000-05  36638.565113
2000-06  18319.282557
2000-07      0.000000
2000-08      0.000000
2000-09      0.000000
2000-10      0.000000
2000-11      0.000000
2000-12      0.000000
2001-01      0.000000
2001-02  18761.849597
2001-03  37523.699193
2001-04  56285.548790
2001-05  37523.699193
2001-06  18761.849597
2001-07      0.000000
2001-08      0.000000
2001-09      0.000000
2001-10      0.000000
2001-11      0.000000
2001-12      0.000000

仅两个数据点实际上是不可能实现的。 公司通常具有多项式增长或指数增长,但是只有两个数据点,您无法拟合如此复杂的增长曲线。 仅线性插值是可能的。

但是,假设您有第三点

import pandas as pd
date = pd.date_range('2000-4-1', periods=3, freq='4Q') # quarter _end_!
Qsales = [54957.84767, 56285.54879, 58277.10047]
df = pd.DataFrame({'Quarter sales':Qsales}, index=pd.Index(date, name='date'))
print(df)
import matplotlib.pyplot as plt
plt.plot(df.index, df['Quarter sales'])
plt.show()

由此可见:

            Quarter sales
date                     
2000-06-30    54957.84767
2001-06-30    56285.54879
2002-06-30    58277.10047

在此处输入图片说明

现在我们可以做点什么。 让我们根据y = offset + factor * base^x拟合指数曲线。 编辑:我在这里使用pd.datetime(2000, 1, 1)作为零点。

#### curve fitting
import numpy as np
date_delta = (date - pd.datetime(2000, 1, 1)) /np.timedelta64(1,'M')
## convert data to x/y
x = date_delta.values
y = df['Quarter sales'].values
## expected function
def expFunc(x, offset, factor, base) : return offset + factor * base**x
## initial guess
guess = (53000, 1000, 1.05)
## call scipy curve fitting
from scipy.optimize import curve_fit
params = curve_fit(expFunc, x, y, guess)
## now first generate data for all quarters using interpolation
# generate new dates
date = pd.date_range('2000-1-1', periods=3*4, freq='Q') # quarter _end_!
date_delta = (date - pd.datetime(2000,1,1)) / np.timedelta64(1, 'M')
x = date_delta.values
Qsales = expFunc(x, params[0][0], params[0][1], params[0][2])
df = pd.DataFrame({'Quarter sales':Qsales}, index=pd.Index(date, name='date'))
print(df)
plt.plot(df.index, df['Quarter sales'])
plt.show()

这给出:

            Quarter sales
date                     
2000-03-31   54702.538666
2000-06-30   54957.847670
2000-09-30   55243.580457
2000-12-31   55560.059331
2001-03-31   55902.585284
2001-06-30   56285.548790
2001-09-30   56714.147971
2001-12-31   57188.866281
2002-03-31   57702.655211
2002-06-30   58277.100470
2002-09-30   58919.999241
2002-12-31   59632.076706

在此处输入图片说明

现在,事情变得顺利了。 但这还不够。 您需要确定每月的销售额。 好吧,因为您现在知道曲线,所以可以根据以下内容分配每月的增长:

#now further interpolate to months
date = pd.date_range('2000-1-1', periods=3*12, freq='M') # month _end_!
date_delta = (date - pd.datetime(2000, 1, 1)) / np.timedelta64(1,'M')
x = date_delta.values
# first determine the exponential factor per month
dateFactors = expFunc(x, params[0][0], params[0][1], params[0][2])
MFactorSeries = pd.Series(dateFactors, index=date)
# now sum the exponential factors to get them for the quarters
QFactorSeries = MFactorSeries.resample('Q').sum()
# and divide them by the quartarly sales to get a monthly sales base 
MSalesBase = np.divide(Qsales, QFactorSeries.values) 
#now some numpy tricks to get the monthly sales
Msales = np.multiply(dateFactors.reshape(12,3), MSalesBase.reshape(12,1)).flatten()
df = pd.DataFrame({'Monthly sales':Msales}, index=pd.Index(date, name='date'))
print(df)
plt.plot(df.index, df['Monthly sales'])
plt.show()

这给出:

            Monthly sales
date                     
2000-01-31   18208.780004
2000-02-29   18233.319078
2000-03-31   18260.439584
2000-04-30   18290.245845
2000-05-31   18319.272021
2000-06-30   18348.329804
2000-07-31   18382.360436
2000-08-31   18414.515147
2000-09-30   18446.704874
2000-10-31   18484.774541
2000-11-30   18519.227954
2000-12-31   18556.056836
2001-01-31   18596.904560
2001-02-28   18632.486725
2001-03-31   18673.193999
2001-04-30   18718.262419
2001-05-31   18761.833781
2001-06-30   18805.452590
2001-07-31   18856.427507
2001-08-31   18904.698469
2001-09-30   18953.021996
2001-10-31   19010.040490
2001-11-30   19061.766635
2001-12-31   19117.059156
2002-01-31   19178.229496
2002-02-28   19231.653416
2002-03-31   19292.772298
2002-04-30   19360.251134
2002-05-31   19425.676408
2002-06-30   19491.172928
2002-07-31   19567.484781
2002-08-31   19639.973435
2002-09-30   19712.541026
2002-10-31   19797.887582
2002-11-30   19875.573492
2002-12-31   19958.615633

在此处输入图片说明

注意

我不是熊猫,scipy,numpy等方面的专家。这就是我应该使用自己的工程背景来做的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM