It is easy to interpolate values in a Pandas.DataFrame
using Series.interpolate
, how can extrapolation be done?
For example, given a DataFrame as shown, how can we extrapolate it 14 more months to 2014-12-31? Linear extrapolation is fine.
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'))
I am thinking that a new DataFrame must first be created, with the DateTimeIndex starting from 2013-11-31 and extending for 14 more M
periods. Beyond that I'm stuck.
Extrapolating a
DataFrame
with aDatetimeIndex
indexThis can be done with two steps:
DatetimeIndex
Extend the Index
Overwrite
df
with a newDataFrame
where the data is resampled onto a new extended index based on original index's start, period and frequency. This allows the originaldf
to come from anywhere, as in thecsv
example case. With this the columns get conveniently filled with NaNs!Extrapolate the data
Most extrapolators will require the inputs to be numeric instead of dates. This can be done with
See this answer for how to extrapolate the values of each column of a
DataFrame
with a 3rd order polynomial.Once the columns are extrapolated, put the dates back