Just tested some of the code above, and it returns a few non expected results:
input:
test_array_input=np.array([1, 3, 4, 5, 4, 9, 7, 2, 4])
output with rolling max (above) yields: [ 1 3 4 5 4 9 7 2] when checking max over 2 fields, i.e. K=1
whereas would expect: [3 4 5 5 9 9 7 4] --> i,.e first field max of 1 & 3
I've implemented:
def udf_rolling_maximum_array(inputarray,numfieldsrolling):
# example: input ( 1 2 3 4 5 6 2 3 4) -- 9 fields
# example: rolling of 2 numfieldsrolling
# example: output ( 2 3 4 5 6 6 3 4) -- 8 fields
# example: rolling of 3 numfieldsrolling
# example: output (3 4 5 6 6 6 4) -- 7 fields
import numpy as np
# setup result array
k=len(inputarray)-numfieldsrolling+1
rollingmax=np.empty(k)
for i in range(k): rollingmax[i] = max(inputarray[i:i+numfieldsrolling])
return rollingmax #return rolling average array as output of UDF
The solutions given above rollingmax,rollingmax1,rollingmax2 in the original post failed my unit test - cf my expectations. See examples in the code text for behaviour I expected. I've not optimised my code for speed - as I dont'have huge data sets.