Recipe: How to convert Strings to DateTimes in Python?
DATA MUNGING PYTHON DATETIME DATA CLEANING PYTHON

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


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from CGI
1 developer from HCL
1 developer from LTI
1 developer from YASH Technologies
1 developer from Actia Group
1 developer from Embicon
1 developer from HvH
1 developer from MudraCircle
1 developer from Altimetrik
1 developer from EXL Service