Keep Only Date Part when Using pandas.to_datetime in Python (Example)

 

On this page, you’ll learn how to keep only the date part when using pandas.to_datetime in the Python programming language.

Table of content:

Let’s do get started!

 

Import pandas Module & Create Example DataFrame

As a first step, we have to import the pandas library to Python:

import pandas as pd

Next, we can create a DataFrame with datetime column as shown below:

data = pd.DataFrame({'date':['1-17-2023 9:13:22',
                             '10-2-2021 10:19:22',
                             '13-3-2022 5:22:12',
                             '11-26-2022 18:1:6',
                             '8-21-2024 3:3:26'],
                    'value':[3, 7, 5, 5,2]})
data['date'] = pd.to_datetime(data['date'])
print(data)
#                  date  value
# 0 2023-01-17 09:13:22      3
# 1 2021-10-02 10:19:22      7
# 2 2022-03-13 05:22:12      5
# 3 2022-11-26 18:01:06      5
# 4 2024-08-21 03:03:26      2

Have a look at the previous console output: It shows that our example DataFrame contains five rows and two columns.

The variable ‘date’ contains date and time values that have been converted to the datetime data type using the pd.to_datetime function.

 

Example: Remove Time Component from datetime Column in pandas DataFrame

This example explains how to retain only the date of a datetime variable.

For this, we can use the date attribute of our date column as shown below:

data_new = data
data_new['date'] = data_new['date'].dt.date
print(data_new)
#          date  value
# 0  2023-01-17      3
# 1  2021-10-02      7
# 2  2022-03-13      5
# 3  2022-11-26      5
# 4  2024-08-21      2

The previous output shows that we have created a new pandas DataFrame that contains only the dates of a datetime column.

 

Video, Further Resources & Summary

Do you need more explanations on how to retain only the date part when using pandas.to_datetime in Python? Then you should have a look at the following video from Corey Schafer’s YouTube channel.

In the video is shown how to work with datetime and time series data in Python.

 

 

Furthermore, you can have a look at the other Python tutorials on Statistics Globe:

This post has shown how to Keep Only Date Part when Using pandas.to_datetime in the Python programming language. In case you have further questions, you may leave a comment below.

 

Gottumukkala Sravan Kumar Statistician & Programmer

Note: This article was created in collaboration with Gottumukkala Sravan Kumar. Gottumukkala is a data analyst and programmer who helps to create tutorials on topics such as the datetime module in Python. You may find more information about Gottumukkala and his other articles on his profile page.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top