Cannot convert date in string format to datetime object

I have a column date which I am trying to convert to date time, so I can extract month from those values. But not all values are getting converted properly and I cannot understand the reason for that. Comparing 1st and 2nd to 3rd value in the sample.

The format for date is DD-MM-YYYY

This is what i have tried.

Here are the values I want to convert

05-02-2019
07-03-2019
18-03-2019
13-02-2019
05-03-2019

sample['Date'] = pd.to_datetime(sample.date)

sample

2 answers

  • answered 2019-04-15 05:57 RAMAN BHATIA

    Try using .strptime() method. Eg :

    date_dt1 = datetime.strptime(date_str1, '%A, %B %d, %Y')
    

    % A - will give you weekday %B - Month name %d - Day of the month %Y - will give you Year

  • answered 2019-04-15 06:01 gmds

    Just specify format:

    pd.to_datetime(sample['date'], format='%d-%m-%Y')