2

My timestamp looks like below in the dataframe of my column but it is in 'object'. I want to convert this into 'timestamp'. How can I convert all values such in my dataframe column into timestamp?

0    01/Jul/1995:00:00:01
1    01/Jul/1995:00:00:06
2    01/Jul/1995:00:00:09
3    01/Jul/1995:00:00:09
4    01/Jul/1995:00:00:09
Name: timestamp, dtype: object

I tried below code referring this stackoverflow post but it gives me error:

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

Below is the error:

ValueError: time data '01/Jul/1995:00:00:01' does not match format '%d/%b/%Y:%H:%M:%S.%f' (match)
Community
  • 1
  • 1
jubins
  • 317
  • 2
  • 7
  • 18

1 Answers1

4

Try the follwing format:

ourdates = pd.to_datetime(df['timestamp'], format='%d/%b/%Y:%H:%M:%S')
Parsa
  • 3,054
  • 3
  • 19
  • 35
  • Thanks you so much! This is working, however is there any way I can keep month names: July, Jan, Feb, etc. – jubins Apr 01 '17 at 21:29
  • @JubinSoni when you convert the string to a datetime object you can output that in whatever format you please. Could be Jul, 07, or July. – Parsa Apr 01 '17 at 21:30
  • But my output now is this: 1995-07-01 00:00:01 and I want '01/Jul/1995 00:00:01'. I tried doing %m instead of %b in format but that gives error. – jubins Apr 01 '17 at 21:34
  • @JubinSoni ok I think this is a separate question to this, but you should find the answer to your second question here: http://stackoverflow.com/questions/30132282/datetime-to-string-with-series-in-python-pandas – Parsa Apr 01 '17 at 21:36