How to convert string variables into DateTime variables in Python?

This recipe helps you convert string variables into DateTime variables in Python

Recipe Objective

Have you ever tried to work on datetime features in a dataset? It may look quite complicated to write datetime in its format, we can write date and time in form of strings but how to convert it in DateTime Stamp.

So this is the recipe on how we can convert string variables into DateTime variables in Python.

Step 1 - Import the library

import pandas as pd

We have imported pandas which will be required.

Step 2 - Creating DataFrame

We have created a datetime dataframe by passing a dictionary with data time written as strings in pd.DataFrame. draw_data = {"date": ["2017-09-01T01:23:41.004053", "2017-10-15T01:21:38.004053", "2017-12-17T01:17:38.004053"], "score": [25, 94, 57]} df = pd.DataFrame(raw_data, columns = ["date", "score"]) print(df); print(df.dtypes)

Step 3 - Converting Strings to DateTime

We have used the function pd.to_datetime to change the feature with dates in it to datatime stamp. df["date"] = pd.to_datetime(df["date"]) print(df); print(df.dtypes)

                         date  score
0  2017-09-01T01:23:41.004053     25
1  2017-10-15T01:21:38.004053     94
2  2017-12-17T01:17:38.004053     57
date     object
score     int64
dtype: object

                        date  score
0 2017-09-01 01:23:41.004053     25
1 2017-10-15 01:21:38.004053     94
2 2017-12-17 01:17:38.004053     57
date     datetime64[ns]
score             int64
dtype: object

Download Materials

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.