Recipe: How to convert string variables into DateTime variables in Python?
DATA MUNGING PYTHON PANDAS DATAFRAME PYTHON DATETIME

How to convert string variables into DateTime variables in Python?

This recipe helps you convert string variables into DateTime variables in Python
In [1]:
## How to convert string variables into DateTime variables in Python
def Kickstarter_Example_79():
    print()
    print(format('How to convert string variables into DateTime variables in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create a dataset with the index being a set of names
    raw_data = {'date': ['2017-09-01T01:23:41.004053', '2017-10-15T01:21:38.004053',
                         '2017-12-17T01:17:38.004053'],
                'score': [25, 94, 57]}
    df = pd.DataFrame(raw_data, columns = ['date', 'score'])
    print(); print(df); print(df.dtypes)

    # convert strings to DateTime
    df["date"] = pd.to_datetime(df["date"])
    print(); print(df); print(df.dtypes)

Kickstarter_Example_79()
********How to convert string variables into DateTime variables in Python*********

                         date  score
0  2017-09-01T01:23:41.004053     25
1  2017-10-15T01:21:38.004053     94
2  2017-12-17T01:17:38.004053     57
date     object
score     int64
dtype: object

                        date  score
0 2017-09-01 01:23:41.004053     25
1 2017-10-15 01:21:38.004053     94
2 2017-12-17 01:17:38.004053     57
date     datetime64[ns]
score             int64
dtype: object


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
3 developers from Infosys
1 developer from Ernst & Young
1 developer from KPMG
1 developer from Sapient
1 developer from Altimetrik
1 developer from HvH
1 developer from LTI
1 developer from Vodafone
1 developer from ANAC
1 developer from ICU Medical