title: Filtering pandas or numpy arrays for continuous series with minimum window length
body: <p>I would like to filter a <code>numpy</code> <code>array</code> (or <code>pandas</code> <code>DataFrame</code>) in a way that only continuous series of the same value with at least <code>window_size</code> length is kept and everything else set to 0.</p>
  
  <p>For example:</p>
  
  <pre><code>[1,1,1,0,0,1,1,1,1,0,0,1,0,0,0,1,1,1,0,1,1,1,1]
  </code></pre>
  
  <p>should become when using a window size of 4</p>
  
  <pre><code>[0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1]
  </code></pre>
  
  <p>I've tried using <code>rolling_apply</code> and <code>scipy.ndimage.filtes.gerneric_filter</code> but due to the nature of rolling kernel functions I don't think it is the right approach here (and I am stuck with it at the moment).</p>
  
  <p>I insert my attempt here anyway:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  import scipy
  #from scipy import ndimage
  df= pd.DataFrame({'x':np.array([1,1,1,0,0,1,1,1,1,0,0,1,0,0,0,1,1,1,0,1,1,1,1])})
  df_alt = df.copy()
  def filter_df(df, colname, window_size):
      rolling_func = lambda z: z.sum() &gt;= window_size
      df[colname] = pd.rolling_apply(df[colname],
                                      window_size,
                                      rolling_func,
                                      min_periods=window_size/2,
                                      center = True) 
  
  def filter_alt(df, colname, window_size):
      rolling_func = lambda z: z.sum() &gt;= window_size
      return scipy.ndimage.filters.generic_filter(df[colname].values,
                                                  rolling_func,
                                                  size = window_size,                                       
                                                  origin = 0)
  
  window_size = 4
  filter_df(df, 'x', window_size)
  print df
  filter_alt(df_alt, 'x', window_size)
  </code></pre>