インデックスDataFrameによる a の外挿DatetimeIndex
これは、次の 2 つの手順で実行できます。
- 拡張する
DatetimeIndex
- データを推定する
インデックスを拡張する
元のインデックスの start 、 period 、および frequency に基づいて、データが新しい拡張インデックスにリサンプリングdfされる場所を新しいもので上書きします。これにより、例の場合のように、オリジナルをどこからでも取得できます。これにより、列は便利に NaN で満たされます!DataFramedfcsv
# Fake DataFrame for example (could come from anywhere)
X1 = range(10)
X2 = map(lambda x: x**2, X1)
df = pd.DataFrame({'x1': X1, 'x2': X2}, index=pd.date_range('20130101',periods=10,freq='M'))
# Number of months to extend
extend = 5
# Extrapolate the index first based on original index
df = pd.DataFrame(
data=df,
index=pd.date_range(
start=df.index[0],
periods=len(df.index) + extend,
freq=df.index.freq
)
)
# Display
print df
x1 x2
2013-01-31 0 0
2013-02-28 1 1
2013-03-31 2 4
2013-04-30 3 9
2013-05-31 4 16
2013-06-30 5 25
2013-07-31 6 36
2013-08-31 7 49
2013-09-30 8 64
2013-10-31 9 81
2013-11-30 NaN NaN
2013-12-31 NaN NaN
2014-01-31 NaN NaN
2014-02-28 NaN NaN
2014-03-31 NaN NaN
データを推定する
ほとんどの外挿法では、入力を日付ではなく数値にする必要があります。これはで行うことができます
# Temporarily remove dates and make index numeric
di = df.index
df = df.reset_index().drop('index', 1)
a の各列の値を 3 次多項式で外挿する方法については、この回答を参照してください。DataFrame
回答の抜粋
# Curve fit each column
for col in fit_df.columns:
# Get x & y
x = fit_df.index.astype(float).values
y = fit_df[col].values
# Curve fit column and get curve parameters
params = curve_fit(func, x, y, guess)
# Store optimized parameters
col_params[col] = params[0]
# Extrapolate each column
for col in df.columns:
# Get the index values for NaNs in the column
x = df[pd.isnull(df[col])].index.astype(float).values
# Extrapolate those points with the fitted function
df[col][x] = func(x, *col_params[col])
列が推定されたら、日付を元に戻します
# Put date index back
df.index = di
# Display
print df
x1 x2
2013-01-31 0 0
2013-02-28 1 1
2013-03-31 2 4
2013-04-30 3 9
2013-05-31 4 16
2013-06-30 5 25
2013-07-31 6 36
2013-08-31 7 49
2013-09-30 8 64
2013-10-31 9 81
2013-11-30 10 100
2013-12-31 11 121
2014-01-31 12 144
2014-02-28 13 169
2014-03-31 14 196