Skip to content
Advertisement

how can I find a date with incorrect Syntax and fix it

I am new to python. I have a dataset I converted it to dataframe. all my dates are objects now. I need to convert them into dates in order to find the age of patients. My dimensions are 3400×14 long. there are date values inside which have incorrect syntax. I cannot find them. is there a way to find them?

Cdf['Birthday'] = Cdf['Birthday'].astype('datetime64[ns]')

I am using this formula to convert. I need date without time. I am getting an error which is “DateParseError: Invalid date specified (25/15)”

Thank you for help in advance

Advertisement

Answer

You can use pd.to_datetime

Cdf['date'] = pd.to_datetime(Cdf['Birthday'], errors='coerce')

and check for which values in this column have NaTs as values. All invalid dates will be converted to NaT. You can use

Cdf.loc[Cdf['date'].isnull(), 'date']

to find all values which are invalid.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement