Last updated: Apr 12, 2024
Reading time·4 min

strftime()To remove time from datetime in Pandas:
pandas.to_datetime() method to convert the string values to
datetime values.dt.date attribute on the datetime values.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)
Running the code sample produces the following output.
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

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.
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.
You can use the dt.date method if you need to remove the time from a single
Pandas datetime object.
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

The date() method returns the date part of a datetime object.
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.
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)
Running the code sample produces the following output.
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

If you access the dtypes attribute on the DataFrame, you will see that the
date column still has a type of datetime64.
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)

strftime()You can also use the
dt.strftime()
method to remove the time from a datetime object.
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)
Running the code sample produces the following output.
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

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.
split()You can also use the
str.split() method to
remove the time component from datetime objects in Pandas.
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)
Running the code sample produces the following output.
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

We split each date string on the space.
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.
You can learn more about the related topics by checking out the following tutorials: