3

I have the column 'Created At' in this form:

The date is in this format: '%d/%m/%Y' -> day, month, year

obj = {'Created At': ['01/01/2017', '01/02/2017', '02/01/2017', 
                      '02/02/2017', 
                      '03/01/2017', '03/02/2017','04/01/2017' ], 
       'Text': [1, 70,14,17,84,76,32]}

df = pd.DataFrame(data=obj)

I did it, but dosen't work:

df.sort_values(by='Created At', inplace=True)

enter image description here It seems that it sorts only the days and disregards the month. What do I do?

1
  • 2
    No it does sort it properly: only in case two dates are the same, it will sort on the month. That is because your dates are strings, and these are sorted lexicographically. Commented Dec 27, 2019 at 21:26

1 Answer 1

7

It does sort it properly: your dates are strings here. Strings are sorted lexicographically. So that means that only if the first character is the same, it will look at the second character, etc.

You therefore might want to convert the column first to datetime objects:

df['Created At'] = pd.to_datetime(df['Created At'], format='%d/%m/%Y')

then we can sort the dataframe, and obtain:

>>> df.sort_values(by='Created At', inplace=True)
>>> df
  Created At  Text
0 2017-01-01     1
2 2017-01-02    14
4 2017-01-03    84
6 2017-01-04    32
1 2017-02-01    70
3 2017-02-02    17
5 2017-02-03    76
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, but i have a quenstion: When you do it: format='%d/%m/%Y' The result is: Year, month, day? For exemple: 2017-02-02 2017-02-03
@Gizelly: the format specifies how we parse the strings to a datetime object. So once it is parsed, the datetime objects, are represented as yyyy-mm-dd. These are not strings anymore, but datetime objects. So it makes calculations, etc. on dates more convenient.
Wow! Thanks for teach me a new concept! I had no idea about lexicography. You helped me so much!
Missing inplace was the issue in my case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.