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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

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.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.