2

I need your help guys

I have information with wrong time format.

For example:

it shows 1245 or 1837 etc. I want them to be like in correct format: 12:45 PM or 6:37 PM.

How can I convert it?

Thanks!

1 Answer 1

2

I think you need convert to_datetime and then strftime or dt.time:

See also http://strftime.org/.

df = pd.DataFrame({'date':[1245, 1837]})
print (df)
   date
0  1245
1  1837

print (pd.to_datetime(df['date'], format='%H%M'))
0   1900-01-01 12:45:00
1   1900-01-01 18:37:00
Name: date, dtype: datetime64[ns]

#for string output
print (pd.to_datetime(df['date'], format='%H%M').dt.strftime('%I:%M %p'))
0    12:45 PM
1    06:37 PM
Name: date, dtype: object

#for time output
print (pd.to_datetime(df['date'], format='%H%M').dt.time)
0    12:45:00
1    18:37:00
Name: date, dtype: object
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.