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

Python – How to sort date string in a Data Frame?

I have a df pandas.core.series.Series such that:

0        11/8/16
1        11/8/16
2        6/12/16
3       10/11/15
4       10/11/15
          ...   
9989     1/21/14
9990     2/26/17
9991     2/26/17
9992     2/26/17
9993     12/30/17

If I use max() on it, it shows the latest date as ‘9/9/17′, instead of the actual maximum which is, in this case ’12/30/17’.

I understand why it is happens – because 9 > 1.

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

How can I fix it?

>Solution :

Convert the dates to timestamps first:

import pandas as pd
df["date"] = pd.to_datetime(df["date"], format="%m/%d/%y")
>>> df["date"].max()
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