Convert column to datetime format, in a leap year

Viewed 292

I'm new to Python and programming in general, so I wasn't able to figure out the following: I have a dataframe named ozon, for which column 1 is the time stamp in mm-dd format. Now I want to change that column to a datetime format using the following code:

ozon[1] = pd.to_datetime(ozon[1], format='%m-%d')

Now this is giving me the following error: ValueError: day is out of range for month. I think it has to do with the fact that it's a leap year, so it doesn't recognize February 29 as a valid date. How can I overcome this error? And could I also add a year to the timestamp (2020)? Thanks so much in advance!

1 Answers

Add year to column and also to format:

ozon[1] = pd.to_datetime(ozon[1] + '-2000', format='%m-%d-%Y')

If still not working because some values are not valid add errors='coerce' parameter:

ozon[1] = pd.to_datetime(ozon[1] + '-2000', format='%m-%d-%Y', errors='coerce')
Related