Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if dates in a pandas column are after a date

I have a pandas dataframe

      date       
0     2010-03  
1     2017-09-14     
2     2020-10-26      
3     2004-12     
4     2012-04-01      
5     2017-02-01      
6     2013-01

I basically want to filter where dates are after 2015-12 (Dec 2015)

To get this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      date       
0     2017-09-14     
1     2020-10-26          
2     2017-02-01  

I tried this

df = df[(df['date']> "2015-12")]

but I’m getting an error

ValueError: Wrong number of items passed 17, placement implies 1

>Solution :

First for me working solution correct:

df = df[(df['date']> "2015-12")]
print (df)
         date
1  2017-09-14
2  2020-10-26
5  2017-02-01

If convert to datetimes, which should be more robust for me working too:

df = df[(pd.to_datetime(df['date'])> "2015-12")]
print (df)
         date
1  2017-09-14
2  2020-10-26
5  2017-02-01

Detail:

print (pd.to_datetime(df['date']))
0   2010-03-01
1   2017-09-14
2   2020-10-26
3   2004-12-01
4   2012-04-01
5   2017-02-01
6   2013-01-01
Name: date, dtype: datetime64[ns]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading