I have a date column in a df. I want to check if the formats are valid or not and then convert it into a particular date format. TO convert I used datetime.strptime but it says that I can't convert as the DateTime doesn't match the format but I gave the exact same format of the column which is checked and converted to a default format using to_datetime. Why am I getting the error what is the best way to convert a column all at once to a particular date format?
code:
df=
Date
0 12-22-2020
1 22-12-2020
3 22122020
4
5 02-22-2020
checking if valid dates or not:
c=pd.to_datetime(df['Date'],errors='coerce')
later I get this:
c:
0 2020-12-22
4 NaT
5 2020-02-22
Now I want to convert this into %m-%d-%Y format.
c=c.to_frame()
c=c.dropna()
datetime.datetime.strptime(str(c['Date']),'%Y-%m-%d').strftime('%m-%d-%Y')
I get error as below:
ValueError: time data '0 2020-12-22\n5 2020-02-22 Name: Date, dtype: datetime64[ns]' does not match format '%Y-%m-%d'
Though it is same format why am I getting this error?