Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Sort a Pandas DataFrame by Date?
Sorting a Pandas DataFrame by date is a common operation in data analysis. Pandas provides several methods to accomplish this, with sort_values() being the most efficient. Before sorting, ensure your date column is in proper datetime format using to_datetime().
Basic Date Sorting with sort_values()
The most straightforward method is using sort_values() after converting string dates to datetime format ?
import pandas as pd
# Create sample DataFrame with date strings
data = {
'Date': ['2023-06-26', '2023-06-24', '2023-06-28', '2023-06-25'],
'Sales': [100, 200, 300, 150]
}
df = pd.DataFrame(data)
# Convert Date column to datetime
df['Date'] = pd.to_datetime(df['Date'])
# Sort by date in ascending order
sorted_df = df.sort_values('Date')
print(sorted_df)
Date Sales
1 2023-06-24 200
3 2023-06-25 150
0 2023-06-26 100
2 2023-06-28 300
Sorting in Descending Order
To sort dates from newest to oldest, set ascending=False ?
import pandas as pd
data = {
'Date': ['2023-06-26', '2023-06-24', '2023-06-28', '2023-06-25'],
'Sales': [100, 200, 300, 150]
}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
# Sort by date in descending order (newest first)
sorted_df = df.sort_values('Date', ascending=False)
print(sorted_df)
Date Sales
2 2023-06-28 300
0 2023-06-26 100
3 2023-06-25 150
1 2023-06-24 200
Using inplace Parameter
Modify the original DataFrame directly using inplace=True instead of creating a new one ?
import pandas as pd
data = {
'Date': ['2023-06-15', '2023-06-14', '2023-06-17', '2023-06-16'],
'Value': [10, 20, 30, 40]
}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
print("Before sorting:")
print(df)
# Sort in-place
df.sort_values('Date', inplace=True)
print("\nAfter sorting:")
print(df)
Before sorting:
Date Value
0 2023-06-15 10
1 2023-06-14 20
2 2023-06-17 30
3 2023-06-16 40
After sorting:
Date Value
1 2023-06-14 20
0 2023-06-15 10
3 2023-06-16 40
2 2023-06-17 30
Alternative Method Using sorted()
You can also use Python's built-in sorted() function with a lambda key ?
import pandas as pd
data = {
'Date': ['2023-06-26', '2023-06-24', '2023-06-28'],
'Sales': [100, 200, 300]
}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date'])
# Sort using sorted() with lambda
sorted_df = pd.DataFrame(
sorted(df.values, key=lambda x: x[0]),
columns=df.columns
)
print(sorted_df)
Date Sales
0 2023-06-24 200
1 2023-06-26 100
2 2023-06-28 300
Comparison of Methods
| Method | Performance | Best For |
|---|---|---|
sort_values() |
Fastest | General date sorting |
sort_values(inplace=True) |
Memory efficient | Modifying original DataFrame |
sorted() |
Slower | Complex custom sorting logic |
Conclusion
Use sort_values() for efficient date sorting in Pandas DataFrames. Always convert date strings to datetime format first using to_datetime(). Use inplace=True to modify the original DataFrame and save memory.
