How to convert STRING to DateTime in Python?
DATA MUNGING

How to convert STRING to DateTime in Python?

How to convert STRING to DateTime in Python?

This recipe helps you convert STRING to DateTime in Python

0
In [1]:
## How to convert STRING to DateTime in Python
def Kickstarter_Example_53():
    print()
    print(format('How to convert STRING to DateTime in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load Libraries
    from datetime import datetime
    from dateutil.parser import parse
    import pandas as pd
    # Create a string variable with a datetime
    date_start = '2012-03-03'
    # Convert the string to datetime format
    print()
    print(datetime.strptime(date_start, '%Y-%m-%d'))
    # Create a list of strings as dates
    dates = ['7/2/2017', '8/6/2016', '11/13/2015', '5/26/2014', '5/2/2013']
    # Use parse() to attempt to auto-convert common string formats
    print()
    print(parse(date_start))

    print()
    print([parse(x) for x in dates])
    # Use parse, but designate that the day is first
    print()
    print(parse(date_start, dayfirst=True))

    # Create a dataframe
    data = {'date': ['2014-05-01 18:47:05.069722', '2014-05-01 18:47:05.119994',
                     '2014-05-02 18:47:05.178768', '2014-05-02 18:47:05.230071',
                     '2014-05-02 18:47:05.230071', '2014-05-02 18:47:05.280592',
                     '2014-05-03 18:47:05.332662', '2014-05-03 18:47:05.385109',
                     '2014-05-04 18:47:05.436523', '2014-05-04 18:47:05.486877'],
            'value': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
    df = pd.DataFrame(data, columns = ['date', 'value'])
    print(df.dtypes)
    # Convert df['date'] from string to datetime
    print()
    print(pd.to_datetime(df['date']))
    print(pd.to_datetime(df['date']).dtypes)
Kickstarter_Example_53()
*******************How to convert STRING to DateTime in Python********************

2012-03-03 00:00:00

2012-03-03 00:00:00

[datetime.datetime(2017, 7, 2, 0, 0), datetime.datetime(2016, 8, 6, 0, 0), datetime.datetime(2015, 11, 13, 0, 0), datetime.datetime(2014, 5, 26, 0, 0), datetime.datetime(2013, 5, 2, 0, 0)]

2012-03-03 00:00:00
date     object
value     int64
dtype: object

0   2014-05-01 18:47:05.069722
1   2014-05-01 18:47:05.119994
2   2014-05-02 18:47:05.178768
3   2014-05-02 18:47:05.230071
4   2014-05-02 18:47:05.230071
5   2014-05-02 18:47:05.280592
6   2014-05-03 18:47:05.332662
7   2014-05-03 18:47:05.385109
8   2014-05-04 18:47:05.436523
9   2014-05-04 18:47:05.486877
Name: date, dtype: datetime64[ns]
datetime64[ns]

Relevant Projects

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

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.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

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.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.

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.