If the numbers in your array are all positive, it is probably simplest to use cumsum()
and then the modulo operator:
>>> a = np.array([1,1,1,1,1,1,1,1,1,1])
>>> limit = 5
>>> x = a.cumsum() % limit
>>> x
array([1, 2, 3, 4, 0, 1, 2, 3, 4, 0])
You can then set any zero values back to the limit to get the desired array:
>>> x[x == 0] = limit
>>> x
array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5])
Here's one possible general solution using Pandas' expanding_apply
method. (I've not tested it extensively...)
First define a modified cumsum
function:
import pandas as pd
def cumsum_limit(x):
q = np.sum(x[:-1])
if q > 0:
q = q%5
r = x[-1]
if q+r <= 5:
return q+r
elif (q+r)%5 == 0:
return 5
else:
return (q+r)%5
a = np.array([1,1,1,1,1,1,1,1,1,1]) # your example array
Apply the function to the array like this:
>>> pd.expanding_apply(a, lambda x: cumsum_limit(x))
array([ 1., 2., 3., 4., 5., 1., 2., 3., 4., 5.])
Here's the function applied to another more interesting Series:
>>> s = pd.Series([3, -8, 4, 5, -3, 501, 7, -100, 98, 3])
>>> pd.expanding_apply(s, lambda x: cumsum_limit(x))
0 3
1 -5
2 -1
3 4
4 1
5 2
6 4
7 -96
8 2
9 5
dtype: float64