0

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  2020-02-04T22:03:44.846000+00:00

I am not sure what is the letter "T" here.

Attempts

pat = '%y-%m-%dT%H:%M%:%SZ'
df['date'] = pd.to_datetime(df['date'],format=pat)

I am not sure what is the correct format here.
2
  • 3
    ISO 8601 Commented Feb 4, 2020 at 22:10
  • @Felipe, thanks I got that, but now struggling the correct datetime format. Commented Feb 4, 2020 at 22:17

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.