title: Iterate over DataFrame Date Groups in order, with reference to previous group
body: <p>I have a MultiIndex (<code>Name</code>, <code>Date</code>) DataFrame <code>df</code> that I need to process iteratively by <code>Date</code> in order to assign a value that is based on both the current and previous Date's Group.</p>
  <p>AFAIK the best way to process DataFrame Groups is by <code>.apply</code> – e.g., <code>df.groupby('Date').apply(ifunc)</code>.</p>
  <p>But how can I best do this when <code>ifunc</code> needs to reference the values from the previous Date Group <em>after that previous Group has been processed by <code>ifunc</code></em>?</p>
  <p>Here is an example of such an <code>ifunc</code> to operate on <code>df</code> with columns <code>['Dollars', 'Weight', 'Return', 'HaveMax']</code>:</p>
  <pre class="lang-py prettyprint-override"><code># (This might not be great python; coding improvements welcome!)
  # Lambda to add &quot;AddDollars&quot; to Names that don't already &quot;HaveMax&quot; &quot;MaxDollars&quot;
  def ifunc(group, previous):  # Arguments are df groups by Date
      group['HaveMax'] = previous['HaveMax']
      # Each Name's Dollars changed from the previous Date
      avgWeights = group['Weight'].mean()
      group['Dollars'] = group['Weight'] * previous['Dollars'] * group['Return'] / avgWeights
      # Now add &quot;AddDollars&quot; to Names that were under
      group.loc[group['HaveMax'] == False, 'Dollars'] = group[group['HaveMax'] == False]['Dollars'] + AddDollars
      # Update HaveMax for any Names that reached MaxDollars on this Date
      group.loc[group['HaveMax'] == False, 'HaveMax'] = group[group['HaveMax'] == False]['Dollars'] &gt;= MaxDollars
      return group
  </code></pre>
  <hr />
  <p>Sample data:</p>
  <pre class="lang-py prettyprint-override"><code>AddDollars = 1.0
  MaxDollars = 10.0
  df = pd.DataFrame(data=[('A', '20210101', 9.0, 1.0, 0, False),
                          ('B', '20210101', 5.0, 1.0, 0, False),
                          ('C', '20210101', 5.0, 1.0, 0, True),
                          ('A', '20210102', 0.0, 1.0, 1.0, False),
                          ('B', '20210102', 0.0, 1.0, 1.0, False),
                          ('C', '20210102', 0.0, 1.0, 1.0, False)],
                    columns=('Name', 'Date', 'Dollars', 'Weight', 'Return', 'HaveMax')).set_index(['Name', 'Date'])
  </code></pre>
  <p>Desired output:</p>
  <pre><code>               Dollars  Weight  Return  HaveMax
  Name Date                                      
  A    20210101      9.0     1.0    0.0    False
  B    20210101      5.0     1.0    0.0    False
  C    20210101      5.0     1.0    0.0     True
  A    20210102     10.0     1.0    1.0     True
  B    20210102      6.0     1.0    1.0    False
  C    20210102      5.0     1.0    1.0     True
  </code></pre>