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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With