Skip to content Skip to sidebar Skip to footer

How To Parse The ISO Format Datetime String In Python?

In pandas how can we make the datetime column from this data? df = pd.DataFrame({'date': ['2020-02-04T22:03:44.846000+00:00']}) print(df) date 0 20

Solution 1:

Thanks to @Felipe,

I got the answer.


df['date'] = pd.to_datetime(df['date'],infer_datetime_format=True)

df = pd.DataFrame({'date': ['2020-02-04T22:03:44.846000+00:00']})

df['year'] = df['date'].dt.year
print(df)

                              date  year
0 2020-02-04 22:03:44.846000+00:00  2020

Solution 2:

You can parse date time string format using this reference: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

import numpy as np
import pandas as pd

pd.options.display.max_columns = 10
pd.set_option('display.max_colwidth', -1)

df = pd.DataFrame({'date': ['2020-02-04T22:03:44.846000+00:00']})

df['date1'] = pd.to_datetime(df['date'],format='%Y-%m-%dT%H:%M:%S.%f%z')
df['date2'] = pd.to_datetime(df['date'],infer_datetime_format=True)
df['hour'] = df['date1'].dt.hour

print(df)

0  2020-02-04T22:03:44.846000+00:00 2020-02-04 22:03:44.846000+00:00   

                             date2  hour  
0 2020-02-04 22:03:44.846000+00:00  22  

Post a Comment for "How To Parse The ISO Format Datetime String In Python?"