Home > Net >  Find/Filter all rows that contains an incorrect date format
Find/Filter all rows that contains an incorrect date format

Time:11-02

I am analyzing some shared data sheets,I set some filters so that this helps to find rows that do not follow the criteria:

filter3 = df[(df['Currency'].isnull())]
filter1= df[(df["Date"] > '2021-06-16' ) & (df['Subtype'].isnull())]

However I have tried to put a filter so when running the script I can find rows that does not follow this date format: %d/%m/%Y

How can I implement this filter? At the end what I would like to do is inform to the person adding rows to that shared report that he/she typed the incorrect format.

Thank you!

CodePudding user response:

Here is an example of how to use the errors parameter of pd.to_datetime. If any of the date values don't adhere to the format it will return null. In this case we use .loc to select the null (invalid) dates.

import pandas as pd

df = pd.DataFrame({'dates':['2021-06-16','11/08/20']})
df.loc[pd.to_datetime(df['dates'], errors='coerce',format='%Y-%m-%d').isnull()]

Output

dates
1   11/08/20
  • Related