How to convert Strings to DateTimes in Python?

How to convert Strings to DateTimes in Python?

How to convert Strings to DateTimes in Python?

This recipe helps you convert Strings to DateTimes in Python


Recipe Objective

Sometimes we get datetime in the format of strings so how to deal with it?

So this is the recipe on how we can convert Strings to DateTimes in Python.

Step 1 - Loading Library

We have imported numpy and pandas which is needed. import numpy as np import pandas as pd

Step 2 - Creating strings

We have created an numpy array of datetime strings. date_strings = np.array(["01-01-2015 11:35 PM", "23-02-2016 12:01 AM", "26-12-2017 09:09 PM"]) print() print(date_strings)

Step 3 - Numerical analysis

We have changed the string in the date time by selecting the format of Date and Time. print([pd.to_datetime(date, format="%d-%m-%Y %I:%M %p", errors="coerce") for date in date_strings]) So the output comes as

["01-01-2015 11:35 PM" "23-02-2016 12:01 AM" "26-12-2017 09:09 PM"]

[Timestamp("2015-01-01 23:35:00"), Timestamp("2016-02-23 00:01:00"), Timestamp("2017-12-26 21:09:00")]

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

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

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

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