How to convert Strings to DateTimes in Python?
0

How to convert Strings to DateTimes in Python?

This recipe helps you convert Strings to DateTimes in Python
In [1]:
## How to convert Strings to DateTimes in Python 
def Kickstarter_Example_49():
    print()
    print(format('How to convert Strings to DateTimes in Python',
                 '*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    import numpy as np
    import pandas as pd

    # Create 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)

    # Convert to datetimes
    print()
    print([pd.to_datetime(date,
                    format="%d-%m-%Y %I:%M %p",
                    errors="coerce") for date in date_strings])
Kickstarter_Example_49()
******************How to convert Strings to DateTimes in Python*******************

['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')]