We can convert DateTime to Date in pandas by using pandas.Series.dt.date
and dt.normalize()
methods. 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, I will explain convert DateTime to date by extracting only date from the date and time column of DataFrame.
Quick Examples of Pandas Convert Datetime to Date
Below are some quick examples of converting the DateTime column to Date.
# Quick examples of convert Datetime to Date
# Example 1: Using Date function
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.date
# Example 2: Using single date function
Datetime = '2021-11-15 21:04:15'
df2 = pd.to_datetime(Datetime).date()
# Example 3: Using normalize() method
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.normalize()
To run some examples of converting DateTime to Date in pandas, let’s create a Pandas DataFrame.
# Create Pandas DataFrame
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee':[22000,25000,23000,24000,26000],
"InsertedDateTime":['2021-11-15 21:04:15','2020-05-04 22:04:10','2018-01-26 15:23:14','2019-02-18 10:05:18','2021-12-10 15:13:21']
})
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
Yields below output.
Convert Datetime to Date
The Datetime column can store the date and time together, If you want just the date and do not want time in format YYYY-MM-DD
format use pandas.Series.dt.date
. Here, pandas.to_datetime() is used to convert the String column to DateTime.
# Using Date function
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.date
print("After converting datetime to date:\n", df)
Yields below output.
If the date in the string column is already in date format, then just use date() to truncate the time.
# Using single date function to get
# date from datetime column
Datetime = '2021-11-15 21:04:15'
df2 = pd.to_datetime(Datetime).date()
print(df2)
# Output:
# 2021-11-15
Convert Datetime to Date Using normalize() Method
You can convert DateTime to date using the normalize()
method. Use pd.to_datetime(df["InsertedDateTime"]).dt.normalize()
method to normalize the data by extracting the date from DateTime.
# Using normalize() method
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.normalize()
print("After converting datetime to date:\n", df)
Yields below output.
# Output:
# After converting datetime to date:
Courses Fee InsertedDateTime Date
0 Spark 22000 2021-11-15 21:04:15 2021-11-15
1 PySpark 25000 2020-05-04 22:04:10 2020-05-04
2 Hadoop 23000 2018-01-26 15:23:14 2018-01-26
3 Python 24000 2019-02-18 10:05:18 2019-02-18
4 Pandas 26000 2021-12-10 15:13:21 2021-12-10
Complete Example Of Convert Datetime to Date
# Example Of Convert Datetime to Date
import pandas as pd
technologies = ({
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee':[22000,25000,23000,24000,26000],
"InsertedDateTime":['2021-11-15 21:04:15','2020-05-04 22:04:10','2018-01-26 15:23:14','2019-02-18 10:05:18','2021-12-10 15:13:21']
})
df = pd.DataFrame(technologies)
print(df)
# Using Date function
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.date
print(df)
# Using single date function
Datetime = '2021-11-15 21:04:15'
df2 = pd.to_datetime(Datetime).date()
print(df2)
# Using normalize() method
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.normalize()
print(df)
Frequently Asked Questions on Convert Datetime to Date
If you have a column of datetime objects in a Pandas DataFrame and you want to convert it to date objects, you can use the dt.date
accessor. For example, df['datetime_column'].dt.date
is used to extract the date portion of the datetime objects in the ‘datetime_column’, and a new column ‘date_column’ is created to store the date objects.
To convert a datetime column to a date column in a Pandas DataFrame, you can use the dt
accessor along with the date
attribute. For example, the pd.to_datetime
function is used to convert the ‘datetime_column’ to a datetime data type. Then, the dt.date
attribute is used to extract the date part and create a new ‘date_column’. Adjust the column names based on your actual DataFrame.
To set the date column as the index of the DataFrame in Pandas, you can use the set_index
method. For example, the set_index
method is used to set the ‘date_column’ as the index of the DataFrame. The inplace=True
parameter modifies the DataFrame in place without the need to create a new variable.
You can convert a datetime column to a date without creating a new column by directly overwriting the existing datetime column. For example, the original ‘datetime_column’ is directly overwritten with the date values using df['datetime_column'] = df['datetime_column'].dt.date
. This modifies the DataFrame in place, and the ‘datetime_column’ now contains date values.
If your datetime column has a different format, you may need to specify the format when using the pd.to_datetime
function. For example, the format
parameter in pd.to_datetime
is used to specify the format of the datetime strings in the ‘datetime_column’. Adjust the format string ('%d-%b-%Y %H:%M:%S'
in this case) based on the actual format of your datetime column.
Conclusion
In this article, I have explained convert DateTime to date in pandas by using pandas.Series.dt.date
and dt.normalize()
, and date()
function and with several examples.
Happy Learning !!
Related Articles
- Pandas Convert Column To DateTime
- How to Format Pandas Datetime?
- Pandas Extract Year from Datetime
- Convert PySpark DataFrame to Pandas
- convert pandas to pyspark dataframe
- Pandas DatetimeIndex Usage Explained
- Convert Pandas DatetimeIndex to String
- pandas Convert Datetime to Seconds
- Sort Pandas DataFrame by Date (Datetime)
- Pandas Get Day, Month and Year from DateTime
- Count(Distinct) SQL Equivalent in Pandas DataFrame
- Select Pandas DataFrame Rows Between Two Dates