Indexing and slicing dataframe by date and time in python
<p>I have time a series datasets. I can select data from march to may by this code:  </p>

<pre><code>df[(df.index.month &gt;=3) &amp; (df.index.month&lt;=5)]
</code></pre>

<p>But the problem is how to select the data from <code>march-15</code> to <code>may-15</code>?
Any help will be highly appreciated.</p>

<p>and my dataframe looks like:</p>

<pre><code>2000-02-25   0.01
2000-02-26   0.03
2000-02-27   1.0
2000-02-28   1.52
2000-02-29   0.23
2000-03-01   0.45
2000-03-05   2.15
2000-03-06   1.75
.................
.................
</code></pre>

<p>You can use helper <code>Series</code> <code>s</code> where all years are replaced to same - e.g. <code>2000</code>:</p>

<pre><code>print (df)
               A
2001-02-25  0.01
2002-02-26  0.03
2003-02-27  1.00
2004-02-28  1.52
2005-03-29  0.23
2006-03-01  0.45
2007-03-05  2.15
2008-03-06  1.75

s = pd.Series(df.index.map(lambda x: pd.datetime(2000, x.month, x.day)))

mask = (s.dt.date &gt; pd.datetime(2000,3,15).date()) &amp; 
       (s.dt.date &lt; pd.datetime(2000,5,15).date())
mask.index = df.index
print (mask)
2001-02-25    False
2002-02-26    False
2003-02-27    False
2004-02-28    False
2005-03-29     True
2006-03-01    False
2007-03-05    False
2008-03-06    False
dtype: bool

df = df[mask]
print (df)
               A
2005-03-29  0.23
</code></pre>

python pandas numpy dataframe