0

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...
James Chen
  • 89
  • 1
  • 8
  • Where's the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve)? – yatu Jun 04 '20 at 17:12
  • 1
    I think `suppr` is a list of arrays, so I guess what you need is `print(np.array(suppr)[:, i1:i2])` and if it is already an array, then `suppr[:, i1:i2]` should be enough. – Ben.T Jun 04 '20 at 17:17
  • @yatu Hi, sorry I tried to make it as readable as possible at first but I guess it was not quite good, I edited it now hopefully it is clearer. Thanks ! @Ben.T Thanks for your reply ! With my edited example above it works great, but with my real array I encounter an error `---> 11 print(suppr[:, i1:i2]) TypeError: list indices must be integers or slices, not tuple` – James Chen Jun 04 '20 at 22:13
  • 1
    @Ben.T I can't edit my comment, but thanks it worked with your other solution. I just needed to change the format of the cells, thanks again a lot ! – James Chen Jun 05 '20 at 14:11
  • @JamesChen glad it works – Ben.T Jun 05 '20 at 14:42
  • @JamesChen can you answer your own question and then accept it, it helps to manage the site :) – Ben.T Jun 05 '20 at 14:44

1 Answers1

0

The solution of @Ben.T juste above worked perfectly !

Quoting : "I think suppr is a list of arrays, so I guess what you need is print(np.array(suppr)[:, i1:i2]) and if it is already an array, then suppr[:, i1:i2] should be enough. "

James Chen
  • 89
  • 1
  • 8