Home > Mobile >  Use datetime module to show which part can't be converted
Use datetime module to show which part can't be converted

Time:04-08

I have a module to test whether user enter wrong birthday like

df1

0    19730505
1    19731027
2    19970230
3         NaN

We can clearly see 2 is wrong because there are no 30th day in February

If I use this statement, it would raise a value error

pd.to_datetime(df1,format="%Y%m%d")
ValueError: day is out of range for month

but I want to output the wrong conversion cell into False

My desired output is

0 True
1 True
2 False
3 False

Which module of datetime can I use here?

CodePudding user response:

Check with

pd.to_datetime(df1,format="%Y%m%d",errors='coerce').notna()
  • Related