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")]

Download Materials

Relevant Projects

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

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.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

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.

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.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.