Remove time from date in pandas

Remove Time from Date in Pandas

In this tutorial, we will look at how to remove the time component from a date in pandas with the help of some examples.

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.

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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'>

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.


Author

  • Piyush Raj profile picture

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    View all posts
Scroll to Top