Python3使用起始值作为最终值的平均值进行推断

时间:2017-09-14 00:35:53

标签: python-3.x sas interpolation extrapolation

我有一个数据框,其中包含一些年度数据,我想推断为每月一次。我有一个在SAS中运行的进程,我正在转向Python3,这样做。它采用年度值并将其用作全年的平均值,将月度值(月份6基本上是值)展开。

这是SAS代码:

proc expand data = interpolation out = interpolate1 from = year to =month;
id MDY;
convert WW_UnempRateFC / observed = average;
convert US_gas/observed = average;
run;

以下是起始数据。

   Year  RW_UnempFC Usgas   
0   2011    7.49    3.40    
1   2012    7.30    3.54    
2   2013    7.10    3.33    
3   2014    7.00    2.90    
4   2015    6.90    2.21    

以下是一年的SAS输出:

JAN2011 7.5054662504    3.1396263397
FEB2011 7.5116185928    3.2054411594
MAR2011 7.5147516515    3.2648661888
APR2011 7.515085202     3.3200225579
MAY2011 7.512602436     3.3689424153
JUN2011 7.5075223604    3.4119533453
JUL2011 7.5000068984    3.4492530654
AUG2011 7.4900880953    3.4816285652
SEP2011 7.4782763777    3.5082981244
OCT2011 7.464603791     3.5300487326
NOV2011 7.4492789568    3.5471936524
DEC2011 7.4324741835    3.5599449231

这是我到目前为止在Python中所拥有的内容。我已经创建了datetime列并将其移动到.interpolate的索引。但是,无论我尝试哪种方法(线性,样条等),我都无法获得相同的输出。

interpolation['Date'] = interpolation['Year'].astype(str).apply(lambda x: 
pd.to_datetime(x, format='%Y'))
interpolation = interpolation.set_index(['Date'])
interpolationexp = interpolation.resample('M').mean().reset_index()
interpolationexp = interpolationexp.interpolate()

第二部分是一些失败的尝试操纵数据以获得不同的东西。比如将值移动到特定月份并从那里线性扩展。

interpolationexp['Year'] = interpolationexp['Year'].apply(np.floor)
interpolation2 = interpolationexp.merge(interpolation, on='Year')
interpolation2['Usgas_y'] = np.where(interpolation2['Date'].month==6, 
interpolation2['Usgas_y'], '')
interpolation2.head(20)

我真的在网上找不到其他关于如何推断类似SAS方法的数字,请帮忙!

0 个答案:

没有答案