1

I have a dataframe created from a .csv document. Since one of the columns has dates, I have used pandas read_csv with parse_dates:

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

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

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

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

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

Is there a way of doing this in one step, more efficiently?

Many thanks!

Pau
  • 312
  • 2
  • 11

1 Answers1

5

You can use the dt accessor:

df = df[df.Dates_column.dt.year == 2014]
IanS
  • 15,771
  • 9
  • 60
  • 84