title: Days since class one was last seen Pandas
body: <p>I have pandas dataframe and I want to calculate number of days since class 1 was last seen per group and add it as a column to the dataframe. How can I do it?</p>
  
  <p>The code below gives dummy data:</p>
  
  <pre><code>from datetime import datetime, timedelta
  import numpy as np
  import pandas as pd
  dates = [datetime.today() + timedelta(delta) for delta in range(20)]
  dates = [datetime.strftime(date, '%Y-%m-%d')[:10] for date in dates]
  df = pd.DataFrame({'date':np.repeat(dates,2),
            'group':['a', 'b']*20,
             'class': np.random.choice([0,1], 40)})  
  df['date'] = pd.to_datetime(df['date'])
  df.head()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/SXeg5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SXeg5.png" alt="enter image description here"></a></p>