Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas: string to datetime

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?

like image 374
Tuutsrednas Avatar asked Apr 08 '26 22:04

Tuutsrednas


1 Answers

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
like image 177
jezrael Avatar answered Apr 10 '26 11:04

jezrael