0

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?

1
  • 1
    considering your other question, what problem are you trying to solve? all date strings in your example are valid, it's just that the day sometimes comes first, sometimes second? Commented Mar 24, 2021 at 19:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

note that pandas by default uses dayfirst=False, so it is assumed that the day comes second. '22-12-2020' just happens to work because 22 is not a valid month. But that is evaluated per entry of the series - so kind of error-prone if not specified in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.