title: Subtracting a rolling window mean based on value from one column based on another without loops in Pandas
body: <p>I'm not sure what the word is for what I'm doing, but I can't just use the pandas rolling (<a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html" rel="nofollow noreferrer">https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.rolling.html</a>) function because the window is not a fixed size in terms of database indices. What I'm trying to do this:</p>
  <p>I have a dataframe with columns UT (time in hours, but not a datetime object) and WINDS, I want to add a third column that subtracts the mean of all WINDS values that are within 12 hours of the time in the UT column. Currently, I do it like this:</p>
  <pre><code>rolsub = []
  
  for i in df['UT']:
      df1 = df[ (df['UT'] &gt; (i-12)) &amp; (df['UT'] &lt; (i+12)) ]
      df2 = df[df['UT'] == i]
      rolsub +=  [float(df2['WINDS'] - df1['WINDS'].mean())]
  
  df['WIND_SUB'] = rolsub
  </code></pre>
  <p>This works fine, but it takes way too long since my dataframe has tens of thousands of entries. There must be a better way to do this, right? Please help!</p>