Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter pandas dataframe based on date value with exact match

I have been trying to filter my data frame for the specific date although the date is present in the data frame but it doesn't return any results

Data in Data frame based on query

df[df['Date'] > '2017-03-20']

returns this results

StaffID     Date        
90047   2017-03-20 19:00:00     
90049   2017-03-20 19:00:00     
90049   2017-03-27 19:00:00     

although when i am running this query

df[df['Date'] == '2017-03-20']

or

df.loc[df['Date'] == '2017-03-20']

it returns me no results at all just an empty data frame

StaffID     Date

my data frame column types are

StaffID                int64
Date          datetime64[ns]

and i have tried above query by comparing data frame date with string as well as by converting the string date into datetime64[ns] still the same results any help please would be appreciated

like image 698
Waqar Avatar asked Sep 27 '17 16:09

Waqar


1 Answers

Use dt.date astype string then compare i.e

df[df['Date'].dt.date.astype(str) == '2017-03-20']

Output:

  StaffID                Date
0    90047 2017-03-20 19:00:00
1    90049 2017-03-20 19:00:00
like image 66
Bharath Avatar answered Oct 12 '22 13:10

Bharath