Skip to content Skip to sidebar Skip to footer

Pandas: Filling Missing Values In Time Series Forward Using A Formula

I have a time series of data in a DataFrame that has missing values at both the beginning and the end of the sample. I'm trying to fill the missing values at the end by growing it

Solution 1:

You can use the below code to do the operation:

A = [np.nan, np.nan, 5.5, 5.7, 5.9, 6.1, 6.0, 5.9, np.nan, np.nan, np.nan]

df = pd.DataFrame({'A': A}, index=pd.date_range(start='2010', periods=len(A), freq="QS"))

for id in df[df.A.isnull() == True].index:
    df.loc[id, 'A'] = 1.5 * df.A.shift().loc[id] - 0.5 * df.A.shift(2).loc[id]

#Output dataframe
                 A
2010-01-01     NaN
2010-04-01     NaN
2010-07-01  5.5000
2010-10-01  5.7000
2011-01-01  5.9000
2011-04-01  6.1000
2011-07-01  6.0000
2011-10-01  5.9000
2012-01-01  5.8500
2012-04-01  5.8250
2012-07-01  5.8125

Post a Comment for "Pandas: Filling Missing Values In Time Series Forward Using A Formula"