0

I have a dataframe column full datetime type that are in the format

2016Oct03:14:38:33

Right now, the data type of this column of the dataframe is String. I would like to convert it into datetime in order to be able perform some numerical operations like subtractions on them. I have tried specifying the format while using pd.to_datetime but as the time is in a 24 hr format, it is throwing up an error. What is the best way to do this? Thanks in advance!

Ruffy26
  • 109
  • 1
  • 12

3 Answers3

4

There doesn't seem to be anything unusual about the time format at all; 24 hour is absolutely standard.

Just the normal strptime is fine:

datetime.strptime(my_date, '%Y%b%d:%H:%M:%S')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

You need to_datetime with parameter format:

df = pd.DataFrame({'dates':['2016Oct03:14:38:33',
                            '2016Oct03:14:38:33',
                            '2016Oct03:14:38:33']})

print (df)
                dates
0  2016Oct03:14:38:33
1  2016Oct03:14:38:33
2  2016Oct03:14:38:33

df.dates = pd.to_datetime(df.dates, format='%Y%b%d:%H:%M:%S')
print (df)
                dates
0 2016-10-03 14:38:33
1 2016-10-03 14:38:33
2 2016-10-03 14:38:33
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Duplicated question

Use datetime.strptime

Ex:

from datetime import datetime

date_object = datetime.strptime('2016Oct03:14:38:33', '%Y%b%d:%H:%M:%S')

Doc : https://docs.python.org/2/library/datetime.html

PyNico
  • 695
  • 5
  • 21