4

I have this column Date_X in pandas dataframe which is a object. I am not able to use pandas to_datetime function here. I want to convert this column to a range of 1 to 365, using which I can carry on my analysis.

29JAN14:21:16:00
01FEB14:00:11:00
30JAN14:15:11:00
01FEB14:00:07:00
29JAN14:19:31:00
30JAN14:23:52:00
29JAN14:19:18:00
30JAN14:16:46:00
30JAN14:21:39:00
31JAN14:17:40:00
01FEB14:00:16:00

1 Answer 1

4

You can use pandas.to_datetime() by providing the format explicitly using the format keyword argument. Example -

import pandas as pd
pd.to_datetime(dataframe['column'],format='%d%b%y:%H:%M:%S')

The explanation for the format -

%d - 2 digit date
%b - 2 letter month abbreviation
%y - 2 digit year
%H - 2 digit hour (24-hour format)
%M - Minutes
%S - Seconds

Demo -

In [46]: pd.to_datetime('29JAN14:21:16:00') #Not working
Out[46]: '29JAN14:21:16:00'

In [48]: pd.to_datetime('29JAN14:21:16:00',format='%d%b%y:%H:%M:%S') #Working
Out[48]: Timestamp('2014-01-29 21:16:00')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! I was working on it for 3 hrs. Greatly relieved :)
Glad I could be helpful :-)

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.