Convert DateTime to date in Python using Pandas
Pandas provide a different set of tools using which we can perform all the necessary tasks on date-time data. Let’s try to understand with the examples discussed below.
Example:
The date value and the DateTime value are both displayed in the output using the print command. The DateTime values are first added to a column of a Pandas DataFrame. The DateTime value is then converted to a date value using the dt.date() function.
Python3
import pandas as pd df = pd.DataFrame({ 'time' : [ '2022-7-16 11:05:00' , '2025-7-18 12:00:30' ]}) print (df) df[ 'time' ] = pd.to_datetime(df[ 'time' ]).dt.date print (df) |
Output:
time 0 2022-7-16 11:05:00 1 2025-7-18 12:00:30 time 0 2022-07-16 1 2025-07-18
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date from Pandas in Python.