Reputation: 1569
I'm trying to convert this time format to datetime:
Date Hour
05/12/17 2:15 p
05/12/17 2:20 p
05/12/17 2:25 p
Using:
pd.to_datetime(df['Hour'], format="%I:%M %p")
Getting this error:
ValueError: time data '05/12/17 2:15 p' does not match format '%d/%m/%y %I:%M %p' (match)
Upvotes: 1
Views: 1987
Reputation: 109546
df = pd.DataFrame({
'Date': ['05/12/17'] * 3,
'Hour': ['2:15 p', '2:20 p', '2:25 a']})
>>> pd.to_datetime(
df['Date']
+ " "
+ df['Hour'].str.replace('p', 'PM').str.replace('a', 'AM')
)
0 2017-05-12 14:15:00
1 2017-05-12 14:20:00
2 2017-05-12 02:25:00
dtype: datetime64[ns]
Note that I changed the third item from 'p' to 'a' to show how it should work for AM. I am also assuming that 'AM' and 'PM' are the correct representations in your locale.
You can add format='%m/%d/%y %I:%M %p'
as an argument to make the conversion more explicit.
Upvotes: 2