Filtering Pandas DataFrames on dates Filtering Pandas DataFrames on dates python python

Filtering Pandas DataFrames on dates


If date column is the index, then use .loc for label based indexing or .iloc for positional indexing.

For example:

df.loc['2014-01-01':'2014-02-01']

See details here http://pandas.pydata.org/pandas-docs/stable/dsintro.html#indexing-selection

If the column is not the index you have two choices:

  1. Make it the index (either temporarily or permanently if it's time-series data)
  2. df[(df['date'] > '2013-01-01') & (df['date'] < '2013-02-01')]

See here for the general explanation

Note: .ix is deprecated.


Previous answer is not correct in my experience, you can't pass it a simple string, needs to be a datetime object. So:

import datetime df.loc[datetime.date(year=2014,month=1,day=1):datetime.date(year=2014,month=2,day=1)]


And if your dates are standardized by importing datetime package, you can simply use:

df[(df['date']>datetime.date(2016,1,1)) & (df['date']<datetime.date(2016,3,1))]  

For standarding your date string using datetime package, you can use this function:

import datetimedatetime.datetime.strptime