Boolean filter using a timestamp value on a dataframe in Python
<p>I have a dataframe created from a <code>.csv</code> document. Since one of the columns has dates, I have used pandas <code>read_csv</code> with <code>parse_dates</code>:</p>

<pre><code>df = pd.read_csv('CSVdata.csv', encoding = "ISO-8859-1", parse_dates=['Dates_column'])
</code></pre>

<p>The dates range from 2012 to 2016. I want to crate a sub-dataframe, containing only the rows from 2014.</p>

<p>The only way I have managed to do this, is with two subsequent Boolean filters:</p>

<pre><code>df_a = df[df.Dates_column&gt;pd.Timestamp('2014')]  # To create a dataframe from 01/Jan/2014 onwards.

df = df_a[df_a.Dates_column&lt;pd.Timestamp('2015')] # To remove all the values after 01/jan/2015
</code></pre>

<p>Is there a way of doing this in one step, more efficiently?</p>

<p>Many thanks!</p>

<p>You can use the <a href="http://pandas.pydata.org/pandas-docs/stable/basics.html#dt-accessor" rel="nofollow"><code>dt</code> accessor</a>:</p>

<pre><code>df = df[df.Dates_column.dt.year == 2014]
</code></pre>

python pandas dataframe timestamp