How to convert object to datetime in Pandas

In this post, you will learn how to convert object to datetime in Pandas.

The problem of incorrect data type is common in the analysis of any type of data. Fortunately, Pandas offers dedicated features that allow you to change the data type to whatever suits you.

How to convert object to datetime using to_datetime

To change the data type from object to datetime I will use the to_datetime function which is already included in Pandas.

Here is my example dataframe which contains columns with dates entered. However, the data format is object.

import pandas as pd

my_df = pd.DataFrame({'id':['id1','id2','id3', 'id2'],
                      'Date1': ['2023-01-01', '2023-04-01', '2023-09-01', '2023-11-01'],
                      'Date2': ['2023-01-02', '2023-04-02', '2023-09-02', '2023-11-02'],
                      'Date3': ['2023-01-03', '2023-04-03', '2023-09-03', '2023-11-03']})

print(my_df.info())

how to convert object to datetime

Here is the code that changes the data format from object to datetime. I used the to_datetime function and used the name of one of my columns as a parameter.

import pandas as pd

my_df = pd.DataFrame({'id':['id1','id2','id3', 'id2'],
                      'Date1': ['2023-01-01', '2023-04-01', '2023-09-01', '2023-11-01'],
                      'Date2': ['2023-01-02', '2023-04-02', '2023-09-02', '2023-11-02'],
                      'Date3': ['2023-01-03', '2023-04-03', '2023-09-03', '2023-11-03']})

my_df['Date1'] = pd.to_datetime(my_df['Date1'], yearfirst = True, format = "%Y-%m-%d")
print(my_df.info())

how to convert object to datetime in Pandas

You can see that the data format changed to datetime64 as expected.

It is worth noting that you parameterize the to_datetime function. I added the exact date format I use in my column. I recommend reading the documentation of the to_datetime function, which I linked below.

See also:
Documentation of to_datetime function

Leave a Reply