Reputation: 5
I want to extract the date from a timestamp column of a CSV file. But it is showing an error: ( time data '1/29/2019 3:30:00.000000000 PM'
does not match format '%m/%d/%Y %I:%M:%S.%f %p')
.
datetime.strptime(my_string,"%m/%d/%Y %I:%M:%S.%f %p")
I have attached the screenshot of the CSV file.
Upvotes: 0
Views: 597
Reputation: 8589
Please try the following:
datetime.strptime(my_string,"%m/%d/%Y %H:%M:%S.%f %p")
Here's the more detailed explanation of your specific case:
%I = Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12
%H = Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
In the moment that your input file presents a 24-hour clock format time your code will throw an error, because you are using %I.
Changing to %H should solve the error.
Here's a link to the complete strptime syntax for Python 2:
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
And Python 3:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
Edit
%f works with microseconds (6 zeroes) but you have 9.
Because of rounding problems my suggestion is to cut out the last 3 digits from your number.
Here's a possible solution:
from datetime import datetime
my_string = "1/29/2019 3:30:00.000000000 PM"
my_string_split = my_string.split()
my_string = my_string_split[0] + " " + my_string_split[1][:-3] + " " + my_string_split[2]
print (datetime.strptime(my_string,"%m/%d/%Y %I:%M:%S.%f %p") )
Output
2019-01-29 15:30:00
Upvotes: 0
Reputation: 5757
You are getting the error because %f
expects a max of 6 digits.
When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right. %f is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available).
But in your example there are 9 zeros.
One work around will be to three 0's to your format string.
>>> x
'1/29/2019 3:30:00.000000000 PM'
>>> datetime.strptime('1/29/2019 3:30:00.000000000 PM', '%m/%d/%Y %H:%M:%S.%f000 %p')
datetime.datetime(2019, 1, 29, 3, 30)
>>> datetime.strptime('1/29/2019 3:30:00.000000000 PM', '%m/%d/%Y %I:%M:%S.%f000 %p')
datetime.datetime(2019, 1, 29, 15, 30)
Upvotes: 2