SSax P
SSax P

Reputation: 23

How to validate a date using Python pandas?

My data frame consists of a column of dates, which are not necessarily in the correct format. for example: 13/13/2020 or 13/10/2011 - if the format is mm/dd/yyyy.

Upvotes: 2

Views: 94

Answers (1)

Dave
Dave

Reputation: 2049

I recommend you use the built in functionality for something like this:

pd.to_datetime(Q4_df['issdt'], infer_datetime_format=True, errors='coerce')

Tune the errors param to get the behavior you want:

errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
   If ‘raise’, then invalid parsing will raise an exception.
   If ‘coerce’, then invalid parsing will be set as NaT.
   If ‘ignore’, then invalid parsing will return the input.

Upvotes: 2

Related Questions