How to Convert Datetime to Date in Pandas ?
DateTime is a collection of dates and times in the format of “yyyy-mm-dd HH:MM:SS” where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime using the Pandas Python module.
Syntax: yyyy-mm-dd HH:MM:SS
Parameters:
- yyyy stands for year
- mm stands for month
- dd stands for date
- HH stands for hours
- MM stands for minutes
- SS stands for seconds
Creating a Sample DataFrame
Here, we are creating a sample DataFrame that we will use further in this article.
Python3
# importing pandas module import pandas as pd # create pandas DataFrame with one column with five # datetime values through a dictionary df = pd.DataFrame({ 'DateTime' : [ '2021-01-15 20:02:11' , '1989-05-24 20:34:11' , '2020-01-18 14:43:24' , '2021-01-15 20:02:10' , '1999-04-04 20:34:11' ]}) # display print (df) |
Output:
DateTime
0 2021-01-15 20:02:11
1 1989-05-24 20:34:11
2 2020-01-18 14:43:24
3 2021-01-15 20:02:10
4 1999-04-04 20:34:11
Convert Datetime to Date in Pandas
Below are the ways by which we can convert Datetime to Date in Pandas:
- Using date function
- Using normalize() method
Convert Datetime to Date in Pandas Using date function
By using date method along with pandas we can get date.
Syntax: dataframe[‘Date’] = pd.to_datetime(dataframe[‘DateTime’]).dt.date
where,
- dataframe is the input dataframe
- to_datetime is the function used to convert datetime string to datetime
- DateTime is the datetime column in the dataframe
- dt.date is used to convert datetime to date
- Date column is the new column to get the date from the datetime
Example 1: Datetime to Date Using Pandas date Function
In this example, a DataFrame with a ‘DateTime’ column containing datetime values is created. Using Pandas, the code converts the ‘DateTime’ column to a new ‘Date’ column containing only the date part, and the updated DataFrame is displayed.
Python3
# importing pandas module import pandas as pd # create pandas DataFrame with one column with five # datetime values through a dictionary df = pd.DataFrame({ 'DateTime' : [ '2021-01-15 20:02:11' , '1989-05-24 20:34:11' , '2020-01-18 14:43:24' , '2021-01-15 20:02:10' , '1999-04-04 20:34:11' ]}) print ( "Original data" ) print (df) # convert datetime column to just date df[ 'Date' ] = pd.to_datetime(df[ 'DateTime' ]).dt.date # display print ( "Only date" ) print (df) |
Output:
Original data
DateTime
0 2021-01-15 20:02:11
1 1989-05-24 20:34:11
2 2020-01-18 14:43:24
3 2021-01-15 20:02:10
4 1999-04-04 20:34:11
Only date
DateTime Date
0 2021-01-15 20:02:11 2021-01-15
1 1989-05-24 20:34:11 1989-05-24
2 2020-01-18 14:43:24 2020-01-18
3 2021-01-15 20:02:10 2021-01-15
4 1999-04-04 20:34:11 1999-04-04
we can also get the datatypes by using dtypes
Example: Python Program to Get the Datatypes
In this example, a DataFrame is created with a ‘DateTime’ column containing datetime values. After converting the ‘DateTime’ column to the date part using pd.to_datetime().dt.date
, the updated DataFrame is displayed. The dtypes
are shown before and after the conversion, highlighting the change in data types.
Python3
# importing pandas module import pandas as pd # create pandas DataFrame with one column with five # datetime values through a dictionary df = pd.DataFrame({ 'DateTime' : [ '2021-01-15 20:02:11' , '1989-05-24 20:34:11' , '2020-01-18 14:43:24' , '2021-01-15 20:02:10' , '1999-04-04 20:34:11' ]}) print ( "---------Original data------------" ) print (df.dtypes) # convert datetime column to just date df[ 'Date' ] = pd.to_datetime(df[ 'DateTime' ]).dt.date # display print ( "--------Only date---------" ) print (df.dtypes) |
Output:
---------Original data------------
DateTime object
dtype: object
--------Only date---------
DateTime object
Date object
dtype: object
Datetime to Date Using normalize() method
we can get by also using normalize() method, this method is used to normalize the data by extracting the date from DateTime. We are using normalize() method to get the data through pandas
Syntax: dataframe[‘Date’] = pd.to_datetime(dataframe[‘DateTime’]).dt.normalize()
Parameters:
- dataframe is the input dataframe
- to_datetime is the function used to convert datetime string to datetime
- DateTime is the datetime column in the dataframe
- dt.normalize() is the function which is used to convert datetime to date
- Date column is the new column to get the date from the datetime
Example: Convert Datetime to Date Using Pandas normalize() Method
In this example, a DataFrame is created with a ‘DateTime’ column containing datetime values. The code then converts the ‘DateTime’ column to a new ‘Date’ column by using the pd.to_datetime().dt.normalize()
method, which extracts only the date part. The updated DataFrame with the extracted date is displayed.
Python3
# importing pandas module import pandas as pd # create pandas DataFrame with one column with five # datetime values through a dictionary df = pd.DataFrame({ 'DateTime' : [ '2021-01-15 20:02:11' , '1989-05-24 20:34:11' , '2020-01-18 14:43:24' , '2021-01-15 20:02:10' , '1999-04-04 20:34:11' ]}) print ( "Original data" ) print (df) # convert datetime column to just date using normalize() # method df[ 'Date' ] = pd.to_datetime(df[ 'DateTime' ]).dt.normalize() # display print ( "date extracted" ) print (df) |
Output:
Original data
DateTime
0 2021-01-15 20:02:11
1 1989-05-24 20:34:11
2 2020-01-18 14:43:24
3 2021-01-15 20:02:10
4 1999-04-04 20:34:11
date extracted
DateTime Date
0 2021-01-15 20:02:11 2021-01-15
1 1989-05-24 20:34:11 1989-05-24
2 2020-01-18 14:43:24 2020-01-18
3 2021-01-15 20:02:10 2021-01-15
4 1999-04-04 20:34:11 1999-04-04
Contact Us