Python

Pandas Sort By Date

“One of the tangible ways to sort the date column of the DataFrame is with a pandas sort by date. This algorithm can sort a DataFrame’s single and multiple date columns. The “to DateTime()” and “sort values()” functions are the two main functions used by pandas to sort by date. We can also display the dates in many ways depending on what we need, whether ascending or descending order is required. Basically, we sort the “date”, “month”, and “year”. The “sort_values()” function is used for sorting the Python date string that will go through the function. When we sort a date by using the “sort_value()” function, it will not harm the data of our DataFrame; it will just modify it according to our requirements.”

The Syntax for the DateTime Function in Pandas

The Syntax for Pandas Sort by Date in Ascending Order

The Syntax for Pandas Sort by Date in Descending Order

Example 1: Displaying How the datetime() and Sort values() Functions Are Being Used in the Pandas Sort by Date

In the first example, we are simply going to use our “to_dgatetime()” and “sort_values()” functions. Firstly, we have imported the library of pandas as “pd” and created our DataFrame as “md”, which could contain a date column. “md” DataFrame includes two declared variables named “serial_no” and “date”. The “serial_no” contains three numeric values, which are “20”, “30”, and “10”. The “Date” variable also stored three values which are “2022-08-15”, “2022-8-16” and “2022-08-14”. When we created our DataFrame with a date column, we just displayed it using the “print()” function.

After the creation of our DataFrame, we applied our main function of the “to_datetime()” function on the “Date” variable. And then, we applied our second “sort_values()” function on the “Date” variable to sort the date of our “md” DataFrame. At the end of the code, we just simply applied our “print()” function to display the sorted date column. Here, we haven’t applied any order restriction. So by default, it sorts the date in ascending order.

Now we will discuss the output of the code which we have displayed. First, the output will display two columns named “serial_no” and “Date” of the DataFrame “md” we created in our code. It has index values between “0” and “2”. “serial_no” column displays three values, “20”, “30”, and “10”, which were assigned to it in the code. The column “Date” displays the date, which is going to be sorted, which are “2022-08-15”, “2022-08-16”, and “2022-08-14”.

When we applied our “to_datetime()” and “sort_values()” function, it just modified our date sorted displayed by the simple “print()” function. After the date became sorted, the index number displayed was “2”, “0”, and “1”. Sequence altered for “serial_no” and “Date” column. The date sequence after sorting comes along with “serial_no” as “10”, “20”, and “30”. The Main column of the date sequence after sorting is “2022-08-14”, “2022-08-15”,s and “2022-08-16”. Here, our DataFrame is sorted by date, as we can see in the display of output.

Example 2: Displaying Pandas Sort by Date for a Single Date Column in Ascending Order

In our second example, we will discuss how we can sort a single date column in “ascending” order. As shown in the display, we imported pandas first as “pd”. We created a DataFrame with the identifier “kd”. Initialized for storing data of “kd”, which are “Names” and “Date of Birth”. The values, which are assigned Two variables are to “Names” are “Quinton”, “Cameron”, “Michael”, “Lance”, and “John”. The values which are assigned to “Date of Birth” are “1995/06/02”, “1990/10/05”, “1996/12/11”, “1992/12/25”, and “1995/06/01”. After that, we used the “print()” function to display our DataFrame “kd”.

After creating our DataFrame, we applied our main function to sort the date column named “Date of Birth” with “.astype()”. After that, we applied the “sort_values()” function along with the verbose “True” in place. After implementing the main function, we just used the “print()” function to display the sorted column of dates in ascending order.

Let’s examine our output display. First, we can see our output displays data of our DataFrame, which we created “kd” having two columns’ “Names” and “Date of Birth”. The index number of the DataFrame ranges from “0” to “4”. The “Names” column displays five names that were assigned in our code to the DataFrame “Quinton”, “Cameron”, “Michael”, “Lance”, and “John”. The column of “Date of Birth” displays five date values in the unsorted form, which are “1995/06/02”, “1990/10/05”, “1996/12/11”, “1992/12/25”, and “1995/06/01”.

We wanted to sort our date column in ascending order. So, after implementation of our main function, “kd.sort_values(by= ‘Date of Birth’, inplace=True)”, our date column is being sorted, as we can see in our display. The “Names” column is also arranged according to the “Date of Birth” column sequence. After sorting by date, our new output display shows five data values of column “Names” as “Cameron”, “Lance”, “John”, “Quinton”, and “Michael” along with their respective “Date of Birth” columns sorted date values as “1990-10-05”, “1992-12-25”, “1995-06-01”, “1995-06-02” and “1996-12-11”. Now, the new data we obtained are sorted in ascending order along with the date column.

Example 3: Displaying Pandas Sort by Date for a Single Date Column in Descending Order

In our third example, we will see how we can perform sort by date in descending order. As from the previous examples, we have first imported a library of pandas as “pd”. Here, we have declared “record” as having two storing variables, “Members Name” and “Birth Date”. The values assigned to “Members Name” are “Rose”, “Ariana”, “Isabel”, “Angela”, and “Cristina”. The values assigned to “Birth Date” are “1998/06/04”, “1996/10/05”, “1999/12/06”, “1997/12/07”, and “2000/06/08”. We created our DataFrame and named it “kd”. We use the “print()” function to display our DataFrame. It will create two columns along with the data, “Members Name” and “Birth Date”.

Now, we will see the main function that we will use to sort our date column in descending order. Here, we implement our main function on the “Birth Date” column to sort it in descending order. We first use “.astype()” on the “Birth Date” column of our DataFrame “kd”. For ordering the date in the “Birth Date” column in descending order we use “kd.sort_values(by= “Birth Date”, ascending=False, inplace=True)”. The “false” verbose has the main role in sorting dates in descending order. After sorting by the date, we just simply use the “print()” function to display our sorted date DataFrame.

Let’s now check our output screen. From the first “0” to “4” index, we can see that it just simply displays the unsorted date data of the DataFrame having two columns, “Members Name” and “Birth Date”. Five names displayed in the column “Members Name” are “Rose”, “Ariana”, “Isabel”, “Angela”, and “Cristina”. The “Birth Date” column displays five dates, which are “1998/06/04”, “1996/10/05”, “1999/12/06”, “1997/12/07”, and “2000/06/08”.

When we implement our main function “sort_values()” along with the “False” verbose, it will sort our date values in descending order. After implementation and based on our output, our dates are sorted in descending order in a sequence. We can see it by checking the index numbers. After sorting, the index number sequence becomes “4”, “2”, “0”, “3” and “1”. The “Birth Date” column is sorted by date in descending order as “2000-06-08”, “1999-12-06”, “1998-06-04”, “1997-12-07” and “1996-10-05”. The sequence of “Members Name” values changed according to their dates as “Cristina”, “Isabel”, “Rose”, “Angela”, and “Ariana”. Here, the new output display shows sort by date in descending order.

Conclusion

In our article, we have explained the pandas sort by date() method by using three examples. In our first example, we have seen how we can simply sort dates by “to_datetime()” and “sort_values()” functions. We just need to assign the “DateTime” type in the right place. In the other two examples, we know how to sort date columns by pandas in ascending and descending order.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.