Sort pandas DataFrame by Date in Python (Example)


In this tutorial, I’ll show how to order the rows of a pandas DataFrame by dates in Python programming.

The post will contain these content blocks:

Let’s start right away…


Example Data & Software Libraries

In order to use the functions of the pandas library, we first have to import pandas.

import pandas as pd                                                      # Import pandas

As a next step, we’ll also have to create some data that we can use in the example syntax below:

data = pd.DataFrame({'dates':['02/25/2023','01/01/2024','17/11/2019','17/10/2022','02/02/2022'],  # Create example DataFrame
                     'values':range(1, 6)})
print(data)                                                              # Print example DataFrame


table 1 DataFrame sort pandas dataframe date python


Have a look at the previous table. It shows that our example data consists of five rows and the two columns “dates” and “values”.


Example: Order pandas DataFrame by Dates Using to_datetime() & sort_values() Functions

The following code illustrates how to reorder the rows of a pandas DataFrame by a date column.

For this, we first should create a copy of our example data, so that we can keep an original version of our data:

data_new = data.copy()                                                   # Create duplicate

In the next step, we have to convert the class of our date column to the datetime class using the to_datetime function. This is important for the sorting process later on, because otherwise Python would sort our data alphabetically.

data_new['dates'] = pd.to_datetime(data_new.dates)                       # Convert to date

Next, we can apply the sort_values function to order our pandas DataFrame according to our date variable:

data_new = data_new.sort_values(by = ['dates'])                          # Sort rows of data

Finally, let’s print our data to the console:

print(data_new)                                                          # Print new data set


table 2 DataFrame sort pandas dataframe date python


The output of the previous Python programming code is shown in Table 2: As you can see, we have sorted our pandas DataFrame by its date column.


Video & Further Resources

For further examples, check out the following video of Corey Schafer’s YouTube channel. In the video, he explains how to sort pandas DataFrames using the Python programming language. You can find the video below:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you may want to read the other Python programming tutorials on my homepage:


This article has illustrated how to sort and rearrange a pandas DataFrame by year, month, and day in a date column in Python programming. Tell me about it in the comments below, in case you have any additional questions. Besides that, please subscribe to my email newsletter in order to receive updates on new tutorials.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.