Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert "month-day" object to date format in pandas dataframe

I have a df with a column:

column
Dec-01

The column datatype is an object.

I am trying to change it to a datatype date.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is what I’ve tried:

df['column'] = pd.to_datetime(df['column']).dt.strftime(%d-%b')

This is the error I receive:

pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: Out of bounds nanosecond timestamp: 1-12-01 00:00:00

Would really appreciate you help 🙏

>Solution :

You can use:

df['date'] = pd.to_datetime(df['column'], format='%b-%d').dt.strftime('%m/%d/22')

NB. you must hardcode the year as it is undefined in your original data and pandas will by default use 1970.

output:

   column      date
0  Dec-01  12/01/22
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading