3

I have a Pandas dataframe with a datetime column in string format. The format is like this:

06 Feb 2014 12:09:42:000

I need to convert this to datetime. Right now I have:

df['date'] = pd.to_datetime(df['STARTDATE'],format='')

My issue is, I do not know what to put in the format argument to parse the string correctly. Can this be done, or is there a better function to use?

1
  • see this strftime.org Commented Jul 10, 2017 at 17:26

1 Answer 1

7

You can check http://strftime.org/ and use:

df['date'] = pd.to_datetime(df['STARTDATE'],format='%d %b %Y %H:%M:%S:%f')

Sample:

df = pd.DataFrame({'STARTDATE':['06 Feb 2014 12:09:42:000','06 Mar 2014 12:09:42:000']})
print (df)
                  STARTDATE
0  06 Feb 2014 12:09:42:000
1  06 Mar 2014 12:09:42:000

df['date'] = pd.to_datetime(df['STARTDATE'],format='%d %b %Y %H:%M:%S:%f')
print (df)
                  STARTDATE                date
0  06 Feb 2014 12:09:42:000 2014-02-06 12:09:42
1  06 Mar 2014 12:09:42:000 2014-03-06 12:09:42
Sign up to request clarification or add additional context in comments.

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.