How to remove Time from DateTime in Pandas [5 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. How to remove Time from DateTime in Pandas
  2. Removing the time from a single Pandas DateTime object
  3. Only changing how the dates are displayed
  4. Remove Time from DateTime in Pandas using strftime()

# How to remove Time from DateTime in Pandas

To remove time from datetime in Pandas:

  1. Use the pandas.to_datetime() method to convert the string values to datetime values.
  2. Access the dt.date attribute on the datetime values.
main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12 09:30:54', '2023-05-23 08:25:44', '2023-09-21 07:15:33' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']).dt.date print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 09:30:54 1 Bobby 2023-05-23 08:25:44 2 Carl 2023-09-21 07:15:33 -------------------------------------------------- name date 0 Alice 2023-01-12 1 Bobby 2023-05-23 2 Carl 2023-09-21

remove time from datetime in pandas

We first used the pandas.to_datetime() method to convert the datetime strings to datetime objects.

The next step is to access the dt.date attribute to get date objects instead of datetime objects.

main.py
df['date'] = pd.to_datetime(df['date']).dt.date # name date # 0 Alice 2023-01-12 # 1 Bobby 2023-05-23 # 2 Carl 2023-09-21 print(df)

The code sample removes the time component from each date in the DataFrame.

# Removing the time from a single Pandas DateTime object

You can use the dt.date method if you need to remove the time from a single Pandas datetime object.

main.py
import pandas as pd dt = pd.Timestamp('2024-04-20 09:30:24') print(dt) # 👉️ 2024-04-20 09:30:24 without_time = dt.date() print(without_time) # 👉️ 2024-04-20

remove time from datetime in pandas

The code for this article is available on GitHub

The date() method returns the date part of a datetime object.

# Only changing how the dates are displayed

If you only want to change how the dates are displayed and don't want to change the dtype (data type), use the dt.normalize() method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12 09:30:54', '2023-05-23 08:25:44', '2023-09-21 07:15:33' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']).dt.normalize() print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 09:30:54 1 Bobby 2023-05-23 08:25:44 2 Carl 2023-09-21 07:15:33 -------------------------------------------------- name date 0 Alice 2023-01-12 1 Bobby 2023-05-23 2 Carl 2023-09-21

only change how dates are displayed

If you access the dtypes attribute on the DataFrame, you will see that the date column still has a type of datetime64.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12 09:30:54', '2023-05-23 08:25:44', '2023-09-21 07:15:33' ] }) df['date'] = pd.to_datetime(df['date']).dt.normalize() # name object # date datetime64[ns] # dtype: object print(df.dtypes)

type still set to datetime64

The code for this article is available on GitHub

# Remove Time from DateTime in Pandas using strftime()

You can also use the dt.strftime() method to remove the time from a datetime object.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12 09:30:54', '2023-05-23 08:25:44', '2023-09-21 07:15:33' ] }) print(df) print('-' * 50) df['date'] = pd.to_datetime(df['date']).dt.strftime('%Y-%m-%d') print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 09:30:54 1 Bobby 2023-05-23 08:25:44 2 Carl 2023-09-21 07:15:33 -------------------------------------------------- name date 0 Alice 2023-01-12 1 Bobby 2023-05-23 2 Carl 2023-09-21

remove time from datetime using stftime

We first converted the strings in the date column to datetime objects and then used the dt.strftime() method to remove the time component.

The method converts the values to Index, using the specified date_format.

You can read more about the strftime method in this section of the docs.

# Remove Time from DateTime in Pandas using split()

You can also use the str.split() method to remove the time component from datetime objects in Pandas.

main.py
import pandas as pd df = pd.DataFrame({ 'name': [ 'Alice', 'Bobby', 'Carl' ], 'date': [ '2023-01-12 09:30:54', '2023-05-23 08:25:44', '2023-09-21 07:15:33' ] }) print(df) print('-' * 50) df['date'] = df['date'].str.split(' ').str[0] print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name date 0 Alice 2023-01-12 09:30:54 1 Bobby 2023-05-23 08:25:44 2 Carl 2023-09-21 07:15:33 -------------------------------------------------- name date 0 Alice 2023-01-12 1 Bobby 2023-05-23 2 Carl 2023-09-21

remove time from date time in pandas using split

We split each date string on the space.

main.py
df['date'] = df['date'].str.split(' ').str[0]

The output of calling split() on each string is a list that contains 2 elements - the date components and the time components.

The last step is to access the list at index 0 to get the date components.

Python indexes are zero-based, so the first item in a list has an index of 0 and the last has an index of -1 or len(a_list) - 1.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.