Have you ever tried to work on datetime features in a dataset? It may look quite complicated to write datetime in its format, we can write date and time in form of strings but how to convert it in DateTime Stamp.
So this is the recipe on how we can convert string variables into DateTime variables in Python.
import pandas as pd
We have imported pandas which will be required.
We have created a datetime dataframe by passing a dictionary with data time written as strings in pd.DataFrame.
draw_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(df); print(df.dtypes)
We have used the function pd.to_datetime to change the feature with dates in it to datatime stamp.
df["date"] = pd.to_datetime(df["date"])
print(df); print(df.dtypes)
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