Vectorized sliding / rolling numpy nanmean

Set those NaNs to 0s and then use np.convolve on those masked versions –

# Window-size
W = 2*n+1

# Non-nans mask
m = ~np.isnan(x)

# "Masked" input array
x0 = np.where(m,x,0)

# Setup conv kernel and perform conv on x0 and mask m for the counts to divide
K = np.ones(W)
out = (np.convolve(x0,K)/np.convolve(m,K))[W-1:]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top