大 pandas :推断尾部的缺失值 [英] pandas: extrapolate missing values at the tail

查看:66
本文介绍了大 pandas :推断尾部的缺失值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框df,它的GDP值也为yyyy-mm TimePeriod索引.

I have a pandas dataframe df, which has GDP values alsong with yyyy-mm TimePeriod index.

import numpy as np
import pandas as pd
import pandas_datareader.data as web
gdp = web.DataReader("GDP", "fred", start, end).resample('M').mean().interpolate(method='linear').round().to_period('M')

Date        GDP
2015-07    16528.0
2015-08    16534.0
2015-09    16541.0
2015-10    16548.0
2015-11    16556.0
2015-12    16564.0
2016-01    16572.0
2016-02    16602.0
2016-03    16633.0
2016-04    16664.0
2016-05    16702.0
2016-06    16740.0
2016-07    16778.0
2016-08    16803.0
2016-09    16827.0
2016-10    16851.0
2016-11    16869.0
2016-12    16886.0
2017-01    16903.0
2017-02    16946.0
2017-03    16988.0
2017-04    17031.0
2017-05    17075.0
2017-06    17120.0
2017-07    17164.0
2017-08        NaN
2017-09        NaN
2017-10        NaN
2017-11        NaN
2017-12        NaN

GDP每季度发布一次.最新数据点是2017年第三季度.因此,我重新采样以获取月度值,并在缺少值时进行插值.如何使用样条曲线或3个月移动平均值等推算出剩余的剩余NaN?我已经看到了一些使用多项式的示例,但是看起来有些过分(多项式的熊猫外推) .我想知道是否有更简单的方法.谢谢!

GDP is published quarterly. The latest data point is 2017 Q3. So I resampled to have monthly values and interpolated when the values were missing. How do I extrapolate to fill up the remaining of the NaN for the rest of the year by using spline or 3 month moving average etc? I have seen some examples using polynomial, but that look like overdoing stuff (pandas extrapolation of polynomial). I was wondering if there is a simpler approach. Thank you!

推荐答案

通过使用内插

df.GDP=df.GDP.interpolate(method='spline', order=2)

df
Out[197]: 
       Date           GDP
0   2015-07  16528.000000
1   2015-08  16534.000000
2   2015-09  16541.000000
3   2015-10  16548.000000
4   2015-11  16556.000000
5   2015-12  16564.000000
6   2016-01  16572.000000
7   2016-02  16602.000000
8   2016-03  16633.000000
9   2016-04  16664.000000
10  2016-05  16702.000000
11  2016-06  16740.000000
12  2016-07  16778.000000
13  2016-08  16803.000000
14  2016-09  16827.000000
15  2016-10  16851.000000
16  2016-11  16869.000000
17  2016-12  16886.000000
18  2017-01  16903.000000
19  2017-02  16946.000000
20  2017-03  16988.000000
21  2017-04  17031.000000
22  2017-05  17075.000000
23  2017-06  17120.000000
24  2017-07  17164.000000
25  2017-08  17211.095399
26  2017-09  17258.357329
27  2017-10  17306.504998
28  2017-11  17355.538404
29  2017-12  17405.457549

数据输入

df
Out[195]: 
       Date      GDP
0   2015-07  16528.0
1   2015-08  16534.0
2   2015-09  16541.0
3   2015-10  16548.0
4   2015-11  16556.0
5   2015-12  16564.0
6   2016-01  16572.0
7   2016-02  16602.0
8   2016-03  16633.0
9   2016-04  16664.0
10  2016-05  16702.0
11  2016-06  16740.0
12  2016-07  16778.0
13  2016-08  16803.0
14  2016-09  16827.0
15  2016-10  16851.0
16  2016-11  16869.0
17  2016-12  16886.0
18  2017-01  16903.0
19  2017-02  16946.0
20  2017-03  16988.0
21  2017-04  17031.0
22  2017-05  17075.0
23  2017-06  17120.0
24  2017-07  17164.0
25  2017-08      NaN
26  2017-09      NaN
27  2017-10      NaN
28  2017-11      NaN
29  2017-12      NaN

这篇关于大 pandas :推断尾部的缺失值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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