One of the common tasks that you might encounter while working with Pandas is converting DateTime to a date string. This requirement often arises when you are only interested in the date part of the DateTime object, excluding the time information. In this article, we will learn about some easy ways to convert a DateTime object to a date string in Pandas.
How to Convert Datetime to Date in Pandas?
Before learning about methods, let’s revise the concept of DateTime. It is a set of dates and times combined into one unit. It follows the format of “yyyy-mm-dd HH:MM:SS”, where “yyyy-mm-dd” represents the date, and “HH:MM:SS” represents the time.
DateTime objects can be used in a lot of places in our projects. We can also query the specific Year, Month, Day, Hour, Minute, or even Second from the object. However, many times we are not able to directly use the DateTime object.
Let us first see an example of creating a DateTime object:
from datetime import datetime # Using the now() method to get current time and date current_time = datetime.now() print(current_time)
Output:
2023-12-28 03:59:30.858756
The datetime.now() function returns a DateTime object representing the current date and time. You can then access various components of the date and time using attributes like year, month, day, hour, minute, second, and microsecond.
When you are grouping data based on date values, converting datetime to date allows you to focus on the date component alone. This is particularly useful when summarizing or analyzing data on a daily, monthly, or yearly basis. Also, when merging dataframes on date columns, conversion of datetime to date can facilitate matching records with the same date, even if they have different time components.
Now, let us discuss the various methods we can use to convert the DateTime to date.
1) Using the .date() Function
The date() function is an in-built function that is used to convert the DateTime object into a date string. It is a simple and direct way to extract the date from a DateTime object.
Let us see an example:
import pandas as pd from datetime import datetime # Create a custom sample dataset data = {'Timestamp': [datetime(2023, 1, 15, 8, 30), datetime(2023, 1, 16, 12, 45), datetime(2023, 1, 17, 10, 0), datetime(2023, 1, 18, 14, 15)]} # Create a DataFrame from the sample dataset df = pd.DataFrame(data) # Display the original DataFrame print("Original DataFrame:") print(df) # Convert Datetime object to date string using .date() df['Date'] = df['Timestamp'].dt.date # Display the DataFrame with the new 'Date' column print("\nDataFrame with Date String:") print(df[['Date']])
Output:
Original DataFrame:
Timestamp
0 2023-01-15 08:30:00
1 2023-01-16 12:45:00
2 2023-01-17 10:00:00
3 2023-01-18 14:15:00
DataFrame with Date String:
Date
0 2023-01-15
1 2023-01-16
2 2023-01-17
3 2023-01-18
2) Using the normalize() Method
Another built-in Python function that we can use for this conversion is the .normalize() method. It is particularly useful when you need to normalize the data by extracting the date from DateTime. Let’s see how it works:
import pandas as pd from datetime import datetime # Create a custom sample dataset data = {'Timestamp': [datetime(2023, 1, 15, 8, 30), datetime(2023, 1, 16, 12, 45), datetime(2023, 1, 17, 10, 0), datetime(2023, 1, 18, 14, 15)]} # Create a DataFrame from the sample dataset df = pd.DataFrame(data) # Display the original DataFrame print("Original DataFrame:") print(df) # Convert Datetime object to date string using .normalize() df['Date'] = df['Timestamp'].dt.normalize() # Display the DataFrame with the new 'Date' column print("\nDataFrame with Date String:") print(df[['Date']])
Output:
Original DataFrame:
Timestamp
0 2023-01-15 08:30:00
1 2023-01-16 12:45:00
2 2023-01-17 10:00:00
3 2023-01-18 14:15:00
DataFrame with Date String:
Date
0 2023-01-15
1 2023-01-16
2 2023-01-17
3 2023-01-18
3) Using a Custom Function
Aside from the built-in functions provided by Pandas, you can also create a custom function to convert Pandas datetime to date. This can be particularly helpful when you have specific requirements that can’t be met by the built-in functions. This method provides more flexibility and control over the formatting of the date, making it a useful tool when working with complex datasets.
The following example will help you better understand it:
import pandas as pd from datetime import datetime # Create a custom sample dataset data = {'Timestamp': [datetime(2023, 1, 15, 8, 30), datetime(2023, 1, 16, 12, 45), datetime(2023, 1, 17, 10, 0), datetime(2023, 1, 18, 14, 15)]} # Create a DataFrame from the sample dataset df = pd.DataFrame(data) # Custom Function to extract the date def extract_date(dt): return dt.date() # Display the original DataFrame print("Original DataFrame:") print(df) # Convert Datetime object to date string using custom function df['Date'] = df['Timestamp'].apply(extract_date) # Display the DataFrame with the new 'Date' column print("\nDataFrame with Date String:") print(df[['Date']])
Output:
Original DataFrame:
Timestamp
0 2023-01-15 08:30:00
1 2023-01-16 12:45:00
2 2023-01-17 10:00:00
3 2023-01-18 14:15:00
DataFrame with Date String:
Date
0 2023-01-15
1 2023-01-16
2 2023-01-17
3 2023-01-18
The next thing you might have to do is to format the date and time in Python.
Conclusion
In this article, we learned about built-in and custom-made methods we can use to convert a DateTime to Date string in Pandas. Date strings are human-readable and provide a clear representation of the date and time information. This can be useful for logging, displaying information to users, or other situations where a human-readable format is preferred.