Skip to content Skip to sidebar Skip to footer

Is There Any Direct Way To Check The Date Format Column By Column In Python(using Datetime)?

I have a date column in a df. Now I want to check if it is matching with a particular format or not by doing column by column. I did it row by row but it takes a lot of time to run

Solution 1:

You should use pandas to_datetime function with errors='coerce' and keep lines correctly converted and lines that were null in the initial column.

Code could be

dates = pd.to_datetime(df['Date'], errors='coerce', format='%m-%d-%Y')
dates = dates[(~dates.isna())|df['Date'].isnull()]

It gives:

02020-12-224NaT52020-02-22

Post a Comment for "Is There Any Direct Way To Check The Date Format Column By Column In Python(using Datetime)?"