0

I would like to know if you could give me a way to calculate rolling means on a numpy masked array, without using the masked values. I currently am using the convolve numpy method, but it does not work:

import numpy as np

a=np.array([1,2,5,4,9,6,1000,3,6,2,9,0])
a=np.ma.masked_where(a>990,a)
print(a)
a=np.convolve(a, np.ones(5), "valid")/5
print(a)

>>>[1 2 5 4 9 6 -- 3 6 2 9 0]
>>>[  4.2   5.2 204.8 204.4 204.8 203.4 204.    4. ]

Thanks a lot for answering.

1 Answers1

2
>>> a = np.array([1,2,5,4,9,6,1000,3,6,2,9,0])
>>> b = np.delete(a, a>990)
>>> window_size = 5
>>> np.convolve(b, np.ones(window_size)/window_size, "valid")
array([4.2, 5.2, 5.4, 5.6, 5.2, 5.2, 4. ])
Woodford
  • 3,746
  • 1
  • 15
  • 29
  • Thanks a lot for answering ! It works. I will also have to adjust my amount of datas both for the array and the datetimes I use to represent them. :) – Greg Madman Aug 03 '21 at 06:45