Parse a Pandas column to Datetime when importing table from SQL database and filtering rows by date

Pandas is aware of the object datetime but when you use some of the import functions it is taken as a string. So what you need to do is make sure the column is set as the datetime type not as a string. Then you can make your query.

df['date']  = pd.to_datetime(df['date'])
df_masked = df[(df['date'] > datetime.date(2012,4,1)) & (df['date'] < datetime.date(2012,4,4))]

You probably need apply, so something like:

df['date'] = df['date'].apply(dateutil.parser.parse)

Without an example of the column I can't guarantee this will work, but something in that direction should help you to carry on.


pandas already reads that as a datetime object! So what you want is to select rows between two dates and you can do that by masking:

df_masked = df[(df.date > '2012-04-01') & (df.date < '2012-04-04')]

Because you said that you were getting an error from the string for some reason, try this:

df_masked = df[(df.date > datetime.date(2012,4,1)) & (df.date < datetime.date(2012,4,4))]