Q:

Averaging over n elements

I have a numpy array like this [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Let's assume I want the average of 3 elements, my target array should then look like this:
[2, 2, 2, 5, 5, 5, 8, 8, 8, 10]

Notice that when there is no triplet available I want to calculate the average over the remaining elements
Is there a neat way to do that with array operations? 

A:

You could reshape the array to use the mean function, for example:
a = np.arange(1,11)
b = a[:a.size//3*3]
b.shape = (-1,3)
c = np.mean(b, axis=1)
# c == array([2.,  5.,  8.]) 

Then reassign the results in the original array:
c.shape = (-1,1)  # i.e. (len(b), 1)
b[:] = c
print(a)
# array([ 2,  2,  2,  5,  5,  5,  8,  8,  8, 10])

Note that this works because b is a sub-view of a. Also the last element is not as you asked the average (I left it untouched), but it'll be easy to fix, with e.g.:
a[9:] = np.mean(a[9:])

