title: Vectorize an iterative algorithm in Pandas. Tried clip, cumsum, ge, diff and all of their combination without any luck so far :)
body: <p>I would like to compute the investment of the trades that happened between two dates (and discard any amount that was on the account before that - which is why it's different from <a href="https://stackoverflow.com/questions/70243569/pandas-cumsum-only-if-positive-else-zero/70243946">this post</a> where they only consider if the result of a given trade is positive or negative).</p>
  <p>So typically I have this situation (3 first columns) and instead of the &quot;Sell&quot; column I want the &quot;Sell adjusted&quot;:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Time t</th>
  <th>Buy</th>
  <th>Sell</th>
  <th>Desired adjusted value for Sell and explanation</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>0</td>
  <td>0</td>
  <td>1</td>
  <td>0 (we didn't buy anything yet)</td>
  </tr>
  <tr>
  <td>1</td>
  <td>0</td>
  <td>2</td>
  <td>0 (same above)</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2</td>
  <td>0</td>
  <td>0 (we are buying so 0)</td>
  </tr>
  <tr>
  <td>3</td>
  <td>3</td>
  <td>0</td>
  <td>0 (we are buying so 0)</td>
  </tr>
  <tr>
  <td>4</td>
  <td>0</td>
  <td>7</td>
  <td>5 (Here, we bought 5 so far, so we can't sell more than that)</td>
  </tr>
  <tr>
  <td>5</td>
  <td>9</td>
  <td>0</td>
  <td>0 (we are buying so 0)</td>
  </tr>
  <tr>
  <td>6</td>
  <td>0</td>
  <td>2</td>
  <td>2 (We currently have 9 on the account, so we can sell 2)</td>
  </tr>
  <tr>
  <td>7</td>
  <td>0</td>
  <td>11</td>
  <td>7 (We now have 7, so that's the max we can sell)</td>
  </tr>
  <tr>
  <td>8</td>
  <td>0</td>
  <td>2</td>
  <td>0 (We have now 0, we can't sell anything)</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have an iterative version, but it takes an awful amount of time to compute and I can't find a good way to vectorize it and that drives me crazy:</p>
  <pre class="lang-py prettyprint-override"><code>
  import pandas as pd
  
  df = pd.DataFrame({'time':[0,1,2,3,4,5,6,7,8], 'nb_bought': [0,0,2,3,0,9,0,0,0], 'nb_sold': [1,2,0,0,7,0,2,11,2]})
  cur_amount = 0
  for index, row in df.iterrows():
     nb_bought = int(row[&quot;nb_bought&quot;])
     nb_sold = int(row[&quot;nb_sold&quot;])
     if nb_bought &gt; 0:
        cur_amount += nb_bought
        df.at[index,&quot;nb_sold_adjusted&quot;] = 0
     elif nb_sold &gt; 0:
        if nb_sold &gt;= cur_amount:
           df.at[index,&quot;nb_sold_adjusted&quot;] = cur_amount
           cur_amount = 0
        else:
           df.at[index,&quot;nb_sold_adjusted&quot;] = nb_sold
           cur_amount -= nb_sold
  
  </code></pre>
  <pre><code>&gt; $ python3 -i tmp.py
  &gt;&gt;&gt; df
     time  nb_bought  nb_sold  nb_sold_adjusted
  0     0          0        1               0.0
  1     1          0        2               0.0
  2     2          2        0               0.0
  3     3          3        0               0.0
  4     4          0        7               5.0
  5     5          9        0               0.0
  6     6          0        2               2.0
  7     7          0       11               7.0
  8     8          0        2               0.0
  </code></pre>