How to convert string variables into DateTime variables in Python?

How to convert string variables into DateTime variables in Python?

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

Relevant Projects

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

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

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.