In this tutorial, we will look at how to remove the time component from a date in pandas with the help of some examples.
How to remove time from date in Pandas?
If you’re working with a pandas datetime type date then you can simply call the .date()
function or if you’re working with a pandas series you can use the .dt.date
attribute to remove the time from the dates. This is similar to getting just the year or month from a pandas date. The following is the syntax:
# remove time from a pandas timestamp object sample_date.date() # remove time from a pandas series of dates df['Date'].dt.date
Note that if the date is not a pandas datetime date, you need to first covert it using pd.to_datetime()
before you can use the dt.date
attribute.
Let’s look at some examples of using the above syntax.
1. Remove time from a pandas date
Let’s first look at how to time from a pandas datetime object. For this, apply the .date()
function.
import pandas as pd sample_date = pd.Timestamp("2020-04-16 08:30:00") # display the date print(sample_date) # remove the time print(sample_date.date())
Output:
2020-04-16 08:30:00 2020-04-16
You can see that we only get the date without its time component. Also note that the type of the returned date is datetime.date
while the original date was a pandas timestamp object.
print(type(sample_date)) print(type(sample_date.date()))
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
<class 'pandas._libs.tslibs.timestamps.Timestamp'> <class 'datetime.date'>
2. Remove time from dates in a pandas column
To remove time from dates in a pandas series, you can use the .dt.date
attribute on the series. Here’s an example –
# create a dataframe df = pd.DataFrame({ 'Date': ["2020-04-16 08:30:00", "2020-07-02 12:37:24", "2021-05-11 18:00:21"] }) # display the dataframe print(df)
Output:
Date 0 2020-04-16 08:30:00 1 2020-07-02 12:37:24 2 2021-05-11 18:00:21
Before we proceed to remove the time, we first need to convert the column to pandas datetime.
# convert to datetime df['Date'] = pd.to_datetime(df['Date'])
Now, let’s add a new column to the dataframe which contains the date without the time part.
# remove time from Date and store it in a new column df['Date_New'] = df['Date'].dt.date # display the dataframe print(df)
Output:
Date Date_New 0 2020-04-16 08:30:00 2020-04-16 1 2020-07-02 12:37:24 2020-07-02 2 2021-05-11 18:00:21 2021-05-11
You can see that resulting dates don’t have time with them. Note that the new column doesn’t have a datetime datatype.
# check the column datatypes df.dtypes
Output:
Date datetime64[ns] Date_New object dtype: object
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.