Convert Column to Datetime in pandas

How to Convert Column to Datetime in pandas : Only 3 Steps

Popular Domains for just 99 Cents at Namecheap!

Do you have a date column that is of string type ? And do you want to convert this column to datetime type. Then this post is for you. Here I will show you various methods for converting a Column to Datatime using Pandas.

 

Steps to Convert Column to Datetime in pandas

Step1: Import the necessary libraries.

In our examples, We are using only pandas libraries. Let’s import them using the import statement.

import pandas as pd

Step 2: Create a Sample Dataframe

Before converting the column to datetime , you have to create a pandas dataframe with at least one column as Date. That column must contains the date of string type. Let’s create them.

Copy, paste, and execute the entire code to create dataframe.

data = {"Date":["12/11/2020","13/11/2020","14/11/2020","15/11/2020"],
"Open":[1,2,3,4],"Close":[5,6,7,8],"Volume":[100,200,300,400]}
df = pd.DataFrame(data=data)

You will get the output dataframe as follow.

Sample Dataframe for Converting Column to Datatime
Sample Dataframe for Converting Column to Datatime

If you will use the df.info() method then  you will know the type of each columns and also the number of rows in that column.

df.info

Output

Printing the Info of Sample Dataframe
Printing the Info of Sample Dataframe

You can see the “Date” column has 4 records.

Step 3: Use the various method to convert Column to Datetime in pandas

In this section, you will know the method to convert the “Date” column to Datetime in pandas. Let’s know about them.

Method 1: Using pandas.to_datetime()

Pandas have an inbuilt function that allows you to convert columns to DateTime. And  it is pd.to_datetime().

Use the following lines of code to convert column to datetime.

df["Date"] = pd.to_datetime(df["Date"]

If you again printout the df then ouptut will look like the same as the sample dataframe creation. But the type of the Date column is different.

df.info()
Printing the Info of Sample Dataframe after apply to_datetime() method
Printing the Info of Sample Dataframe after apply to_datetime() method

Method 2 : Using the astype() method.

The second method to convert Column to Datetime in pandas is applying astype(“datetime64[ns]”). It changes the type of the  “Date” column to datetime.

Execute the following lines of code to change it.

df["Date"] = df["Date"].astype('datetime64[ns]')

Now if you will printout the info of this dataframe you will get the output as below.

Printing the Info of Sample Dataframe after apply to_datetime() method
Printing the Info of Sample Dataframe after apply to_datetime() method

 

Method 3: Applying the lambda function with apply() function.

The third method to Convert Column to Datetime in pandas is applying the lambda function on each “Date” column value. It can be done using the apply() method. And at last you have to pass the lambda function.

Run the lines of code below.

df["Date"] = df["Date"].apply(lambda d : pd.to_datetime(d))

Now if you print the info of the dataframe. Then you will see that “Date” Column has converted from string to datetime.

Printing the Info of Sample Dataframe after apply to_datetime() method
Printing the Info of Sample Dataframe after applying to_datetime() method

Conclusion

These are the three methods that allow you to Convert Column to Datetime in pandas. It’s your choice to use any of the three methods.  If in your dataset the “date” column is in datetime64 and want to convert it to datetime. Then read Numpy datetime64 to datetime and Vice-Versa implementation.

Hope you have liked this article. If you have any questions then you can contact us for more information.

Source:

pandas.Dataframe.apply() 

pandas.to_datetime()

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner