title: How to get the mean of nsmallest from Pandas DataFrame using Datetimes
body: <p>I am trying to get the nsmallest and the mean of nsmallest from a dataframe by selecting dates. </p>
  
  <p>From this DataFrame: </p>
  
  <pre><code>                    2       chk2       chk3     val
  0                                                  
  2016-08-01  31.340000 2016-05-09 2016-08-08  18.605
  2016-08-02  32.359999 2016-05-09 2016-08-08  18.605
  2016-08-03  32.089001 2016-05-09 2016-08-08  18.605
  2016-08-04  31.194001 2016-05-09 2016-08-08  18.605
  2016-08-05  30.585000 2016-05-09 2016-08-08  18.605
  2016-08-08  20.490000 2016-05-09 2016-08-08  18.605
  2016-08-09  20.135000 2016-08-08 2016-11-21  18.605
  2016-08-10  19.103000 2016-08-08 2016-11-21  18.605
  2016-08-11  19.452000 2016-08-08 2016-11-21  18.605
  2016-08-12  19.241001 2016-08-08 2016-11-21  18.605
  2016-08-15  19.645000 2016-08-08 2016-11-21  18.605
  2016-08-16  20.124000 2016-08-08 2016-11-21  18.605
  2016-08-17  19.863001 2016-08-08 2016-11-21  18.605
  2016-08-18  19.667999 2016-08-08 2016-11-21  18.605
  2016-08-19  19.083001 2016-08-08 2016-11-21  18.605
  2016-08-22  18.163000 2016-08-08 2016-11-21  18.605
  2016-08-23  18.948001 2016-08-08 2016-11-21  18.605
  2016-08-24  19.329999 2016-08-08 2016-11-21  18.605
  2016-08-25  19.735999 2016-08-08 2016-11-21  18.605
  2016-08-26  19.769999 2016-08-08 2016-11-21  18.605
  2016-08-29  18.704000 2016-08-08 2016-11-21  18.605
  2016-08-30  19.756000 2016-08-08 2016-11-21  18.605
  2016-08-31  19.931000 2016-08-08 2016-11-21  18.605
  </code></pre>
  
  <p>This gives me <code>nsmallest</code> of the entire Dataframe, seems to ingore the dates in <code>chk2</code> and <code>chk3</code>  have changed after the first week </p>
  
  <pre><code>df.query('chk2 &lt;= index &lt;= chk3')[2].nsmallest(3)
  
  0
  2016-08-22    18.163000
  2016-08-29    18.704000
  2016-08-23    18.948001
  Name: 2, dtype: float64
  </code></pre>
  
  <p>Applying this function -- it seems to ingore the change in dates in the first week.</p>
  
  <pre><code>def _test(row):
  #     df.query('chk2 &lt;= index &lt;= chk3')[2].nsmallest(3).mean()
      return df.query('chk2 &lt;= index &lt;= chk3')[2].nsmallest(3).mean()
  
      #return df.query('row[1] &lt;= index &lt;= row[2]')[2].nsmallest(3).mean() 
      #UndefinedVariableError: ("name 'row' is not defined", u'occurred at index 2016-08-01 00:00:00')
  
  
  df.info()
  &lt;class 'pandas.core.frame.DataFrame'&gt;
  DatetimeIndex: 23 entries, 2016-08-01 to 2016-08-31
  Data columns (total 3 columns):
  2       23 non-null float64
  chk2    23 non-null datetime64[ns]
  chk3    23 non-null datetime64[ns]
  dtypes: datetime64[ns](2), float64(1)
  memory usage: 736.0 bytes
  </code></pre>