I had an issue with cumsum in a dataframe, which was nicely resolved here : https://stackoverflow.com/a/61842690/7937578
But when I tried to do it with my entire dataframe, I couldn't fit all my data into pandas, so I tried converting it to numpy arrays only, but I can't seem to reproduce the code in numpy only. So far I have this :
test = np.arange(200).reshape(4, 50)
test[2] = np.random.choice([-1, 0, 1], size=50)
TARGET_SUM = 10
x = np.cumsum(test[2] != 0)
changing = np.roll(x, 1) != x
indices = np.where(changing & (x % TARGET_SUM == 0) & (x > 0))[0]
indices = np.concatenate(([-1,], indices))
indices += 1
for i1, i2 in zip(indices[0:-1], indices[1:]):
print(i1, i2)
print(test[i1:i2])
But the output is this :
0 13
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49]
[ 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
86 87 88 89 90 91 92 93 94 95 96 97 98 99]
[ -1 1 0 -1 -1 0 0 -1 1 -1 1 1 -1 -1 0 -1 0 0
0 -1 0 0 -1 1 -1 1 1 -1 -1 0 1 0 0 -1 1 -1
1 0 0 0 1 0 -1 1 1 1 1 1 1 1]
[150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
186 187 188 189 190 191 192 193 194 195 196 197 198 199]]
13 29
[]
29 46
[]
Where it should be more like this :
0 13
[[ 0 1 2 3 4 5 6 7 8 9 10 11 12]
[ 50 51 52 53 54 55 56 57 58 59 60 61 62]
[ -1 1 0 -1 -1 0 0 -1 1 -1 1 1 -1]
[ 150 151 152 153 154 155 156 157 158 159 160 161 162]]
13 29
etc...