Replace NaN or missing values ​​with moving average or other interpolation

I have a pandas framework with monthly data that I want to calculate for a 12-month moving average. However, data for each month of January is missing (NaN), so I use

pd.rolling_mean(data["variable"]), 12, center=True)

but he just gives me all the NaN values.

Is there an easy way to ignore NaN values? I understand that in practice this will become an 11-month moving average.

There are other variables in the data frame that have January data, so I don’t want to just throw out the January columns and make an 11-month moving average.

+4
source share
2 answers

, , . , , , () .

df=pd.DataFrame({ 'month' : [10,11,12,1,2,3],
                  'temp'  : [65,50,45,np.nan,40,43] }).set_index('month')

, , , , , - . , 3, , . ( min_periods=1, @user394430.)

df['rollmean12'] = df['temp'].rolling(12,center=True,min_periods=1).mean()
df['rollmean3']  = df['temp'].rolling( 3,center=True,min_periods=1).mean()

, . , update() (. ).

df['update'] = df['rollmean3']
df['update'].update( df['temp'] )  # note: this is an inplace operation

, , , .

df['ffill']   = df['temp'].ffill()         # previous month 
df['bfill']   = df['temp'].bfill()         # next month
df['interp']  = df['temp'].interpolate()   # mean of prev/next

interpolate() , intepolation. . pandas . statck:   DataFrame pandas

:

       temp  rollmean12  rollmean3  update  ffill  bfill  interp
month                                                           
10     65.0        48.6  57.500000    65.0   65.0   65.0    65.0
11     50.0        48.6  53.333333    50.0   50.0   50.0    50.0
12     45.0        48.6  47.500000    45.0   45.0   45.0    45.0
1       NaN        48.6  42.500000    42.5   45.0   40.0    42.5
2      40.0        48.6  41.500000    40.0   40.0   40.0    40.0
3      43.0        48.6  41.500000    43.0   43.0   43.0    43.0

, , "update" "interp" . , , .

+5

min_periods=1. , 18, Rolling object.

data["variable"].rolling(min_periods=1, center=True, window=12).mean().

+7

All Articles