How to convert float into date and time in pandas

Viewed 13

I have a date column in my dataframe that I want to use at an index in that dataframe. I tried to convert this columns using pd.to_datetime() but it did not work.

my columns containing date looks like:

0        9.2017
1       10.2017
2       11.2017
3       12.2017
4        1.2018

Error that I am getting is:

ValueError: time data '9' does not match format '%m.%YYYY' (match)

Is that related with formating or I need to use some other liberary? Looking for some help/suggestion.

1 Answers

you need to convert the column to string first:

pd.to_datetime(df['date_col'].astype(str), format='%m.%Y')
Related