title: Pandas Panel Data - Returns rolling cumulative sum with year gaps
body: <p>I am currently working with a panel data of financial information on pandas, and I am trying to generate a column of cumulative abnormal returns for 3-year on a rolling basis. Unfortunately my data is a bit spotty and therefore for the same company I might have a gap in the years. This means that I can not simply apply <code>.rolling(3).sum()</code> because we risk of adding years that do not belong with one another. Just to give you an idea, here is an example of my df:</p>
  
  <pre><code>       datadate    fyear       tic   ab_ret   
  0    31/12/1998     1998      AAPL    0.045  
  1    31/12/1999     1999      AAPL    0.012   
  2    31/12/2002     2002      AAPL   -0.031   
  3    31/12/2003     2003      AAPL   -0.007   
  4    31/12/2004     2004      AAPL    0.056
  5    31/12/2005     2005      AAPL    0.001   
  6    31/05/2008     2008      TSLA    0.034    
  7    31/05/2009     2009      TSLA    0.061    
  8    31/05/2010     2010      TSLA    0.003    
  9    31/05/2011     2011      TSLA   -0.004    
  10   31/05/2014     2014      TSLA    0.009  
  ..      ...         ..         ..      ..      
  </code></pre>
  
  <p>this is the result that I am looking to have: </p>
  
  <pre><code>       datadate    fyear       tic    ab_ret   cum_ab
  0    31/12/1998     1998      AAPL    0.045      NaN
  1    31/12/1999     1999      AAPL    0.012      NaN
  2    31/12/2002     2002      AAPL   -0.031      NaN
  3    31/12/2003     2003      AAPL   -0.007      NaN
  4    31/12/2004     2004      AAPL    0.056    0.018
  5    31/12/2005     2005      AAPL    0.001    0.050
  6    31/05/2008     2008      TSLA    0.034      NaN    
  7    31/05/2009     2009      TSLA    0.061      NaN
  8    31/05/2010     2010      TSLA    0.003    0.098
  9    31/05/2011     2011      TSLA   -0.004    0.060
  10   31/05/2014     2014      TSLA    0.009      NaN
  ..      ...         ..         ..      ..       ..
  </code></pre>
  
  <p>I have tried the following code:</p>
  
  <pre><code>df['cum_ab'] = np.nan
  mask = df.groupby('tic')['fyear'].apply(lambda x: x.shift(1)==x-1)
  df.loc[mask,'cum_ab'] = df.groupby('tic')['ab_ret'].rolling(3).sum()
  </code></pre>
  
  <p>But unfortunately it doesn't seem to work as I get the following error: <code>ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long'</code>. </p>
  
  <p>Thank you in advance for your help :) </p>