Toi
Toi

Reputation: 137

How do we convert a date into a particular format using DATETIME(python)?

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?

Upvotes: 0

Views: 74

Answers (1)

Wiliam
Wiliam

Reputation: 1088

Given you have the following dataframe:

df = pd.DataFrame({'Date':['12-22-2020','22-12-2020','22122020',np.nan, '02-22-2020']})

you can do :

df1 = pd.to_datetime(df['Date'],errors='coerce')

which gives:

0   2020-12-22
1   2020-12-22
2          NaT
3          NaT
4   2020-02-22

you see that you have two Nan now as one is from yours and the other is a string 22122020. Now if you do

df1.dt.strftime('%m/%d/%Y')

you get:

0    12/22/2020
1    12/22/2020
2           NaN
3           NaN
4    02/22/2020

which is the format you want.

Upvotes: 2

Related Questions