title: Binning data into equal box sizes and apply OLS to each bin
body: <p>I have a DataFrame <code>df1</code>:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  import statsmodels.formula.api as sm
  
  df1 = pd.DataFrame( np.random.randn(3000,1), index= pd.date_range('1/1/1990', periods=3000), columns = {"M"})
  </code></pre>
  
  <p>I would like to group elements in a box size = 10, fit them using OLS and compute <code>Y_t</code>, where <code>Y_t</code>stands for the series of straight line fits.</p>
  
  <p>In other words, I would like to take the first 10 values, fit them using OLS ( Y_t = b*X_t+a_0) and obtain the values <code>Y_t</code> for these 10 values. Again do the same for the next 10 values (not a rolling window!), and so on and so forth.  </p>
  
  <p><strong>My approach</strong></p>
  
  <p>The first issue that I faced was that I could not fit elements using <code>DateTime</code> values as predictors, so I defined a new DataFrame <code>df_fit</code> that contains two columns <code>A</code>and <code>B</code>. Column <code>A</code>contains integers from 0 to 9, and column <code>B</code>the values of <code>df1</code> in groups of 10 elements:</p>
  
  <pre><code> def compute_yt(df,i,bs):
  
      df_fit = pd.DataFrame({"B": np.arange(1,bs+1),\
                             "A": df.reset_index().loc[i*bs:((i+1)*bs-1), "M"]})
  
      fit = sm.ols(formula = "A ~ B", data = df_fit).fit()
      yt = fit.params.B*df_fit["B"] + fit.params.Intercept
  
      return yt
  </code></pre>
  
  <p>Where <code>bs</code> is the box size (10 in this example), <code>i</code>is an index that allows to sweep over all values. </p>
  
  <p>Finally, </p>
  
  <pre><code> result = [compute_yt(df1,n,l) for n in np.arange(0,round(len(df1)/l)-1)]           
  
   result =    
        Name: B, dtype: float64, 840   -0.249590
        841   -0.249935
        842   -0.250280
        843   -0.250625
        844   -0.250970
        845   -0.251315
        846   -0.251660
        847   -0.252005
        848   -0.252350
        849   -0.252695
        Name: B, dtype: float64, 850   -0.252631
        851   -0.252408
        ...    ... 
  </code></pre>
  
  <p>Where <code>result</code>is a list that should contain the values for the straight line fits. </p>
  
  <p>So, my questions are the following:</p>
  
  <ol>
  <li><p>Is there a way to run an OLS using DateTime values as predictors?</p></li>
  <li><p>I would like to use the list comprehension to build a DataFrame (with the same shape as <code>df1</code>) containing the values of <code>y_t</code>. This relates to question (1) in the sense that I would like to obtain a time-series for these values. </p></li>
  <li><p>Is there a more "pythonic" way to write this code? The way I have sliced the dataframe does not seem too much suitable.</p></li>
  </ol>