Is there a similar function to scipy.ndimage's uniform\_filter or convolve (similar problem with [Numpy Two-Dimensional Moving Average](https://stackoverflow.com/questions/23000260/numpy-two-dimensional-moving-average)), but the average is calculated using only the values in the input array (disregard count of fill values for corner and edge cells). A similar function wherein the fill value can be set to np.nan and the resulting average is calculated as np.nanmean?

My initial code loops over the array to get the neighbors and calculate the resulting mean, but this method takes too long. I have tried both uniform\_filter or convolve, but the results are not what I needed since the resulting corner and edge values are too low (due to filling the edges with 0).

For example, if I have the array:

```py
a = np.ones((4,5))

```

Calculating the mean value from a moving 3x3 array should also result to:

```py
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])

```

uniform\_filter gives:

```py
uniform_filter(a, size=3, mode='constant', cval=0.0)

array([[0.44444444, 0.66666667, 0.66666667, 0.66666667, 0.44444444],
       [0.66666667, 1.        , 1.        , 1.        , 0.66666667],
       [0.66666667, 1.        , 1.        , 1.        , 0.66666667],
       [0.44444444, 0.66666667, 0.66666667, 0.66666667, 0.44444444]])

```

I have tried setting cval=np.nan but the resulting values of the cell edges are nan.

For another array, b

```py
array([[1., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

```

I want to obtain an array, using a 3x3 averaging-window, as

```py
array([[0.25      , 0.16666667, 0.        ],
       [0.16666667, 0.11111111, 0.        ],
       [0.        , 0.        , 0.        ]])

```

The values are calculated as shown in this 
[illustration](https://i.stack.imgur.com/GIRa9.png)