title: Pandas Rolling Computations on Sliding Windows (Unevenly spaced)
body: <p>Consider you've got some unevenly time series data:</p>
  
  <pre><code>import pandas as pd
  import random as randy
  ts = pd.Series(range(1000),index=randy.sample(pd.date_range('2013-02-01 09:00:00.000000',periods=1e6,freq='U'),1000)).sort_index()
  print ts.head()
  
  
  2013-02-01 09:00:00.002895    995
  2013-02-01 09:00:00.003765    499
  2013-02-01 09:00:00.003838    797
  2013-02-01 09:00:00.004727    295
  2013-02-01 09:00:00.006287    253
  </code></pre>
  
  <p>Let's say I wanted to do the rolling sum over a 1ms window to get this:</p>
  
  <pre><code>2013-02-01 09:00:00.002895    995
  2013-02-01 09:00:00.003765    499 + 995
  2013-02-01 09:00:00.003838    797 + 499 + 995
  2013-02-01 09:00:00.004727    295 + 797 + 499
  2013-02-01 09:00:00.006287    253
  </code></pre>
  
  <p>Currently, I cast everything back to longs and do this in cython, but is this possible in pure pandas? I'm aware that you can do something like .asfreq('U') and then fill and use the traditional functions but this doesn't scale once you've got more than a toy # of rows.</p>
  
  <p>As a point of reference, here's a hackish, not fast Cython version:</p>
  
  <pre><code>%%cython
  import numpy as np
  cimport cython
  cimport numpy as np
  
  ctypedef np.double_t DTYPE_t
  
  def rolling_sum_cython(np.ndarray[long,ndim=1] times, np.ndarray[double,ndim=1] to_add, long window_size):
      cdef long t_len = times.shape[0], s_len = to_add.shape[0], i =0, win_size = window_size, t_diff, j, window_start
      cdef np.ndarray[DTYPE_t, ndim=1] res = np.zeros(t_len, dtype=np.double)
      assert(t_len==s_len)
      for i in range(0,t_len):
          window_start = times[i] - win_size
          j = i
          while times[j]&gt;= window_start and j&gt;=0:
              res[i] += to_add[j]
              j-=1
      return res   
  </code></pre>
  
  <p>Demonstrating this on a slightly larger series:</p>
  
  <pre><code>ts = pd.Series(range(100000),index=randy.sample(pd.date_range('2013-02-01 09:00:00.000000',periods=1e8,freq='U'),100000)).sort_index()
  
  %%timeit
  res2 = rolling_sum_cython(ts.index.astype(int64),ts.values.astype(double),long(1e6))
  1000 loops, best of 3: 1.56 ms per loop
  </code></pre>