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.

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

Leave a Reply