Home > OS >  Filter dataframe for non date rows and blank values
Filter dataframe for non date rows and blank values

Time:07-24

I have a dataframe as following:

    Person Type Hire Date   Original Hire Date
 0   Employee   2010-12-01  2010-12-01
 1   Employee   2011-03-13  xxx
 2   Employee               2012-01-02
 3   employee   ?           2
 4   Employee   2012-12-19  2012-12-19
 5   Employee   2013-02-07  2013-02-07

I need to find a way to filter this entire dataframe in a way that it returns all rows not in date or datetime format. The output expected would be:

    Person Type Hire Date   Original Hire Date
1    Employee   2011-03-13  xxx
2    Employee               2012-01-02
3    employee   ?           2

What would be the best way to do this filtering? Thank you so much!

CodePudding user response:

Filter the date like columns then parse the columns to datetime and use isna any to find the rows which have one or more values in non datetime format

cols = df.filter(like='Date')
df[cols.apply(pd.to_datetime, errors='coerce').isna().any(1)]

  Person Type   Hire Date Original Hire Date
1    Employee  2011-03-13                xxx
2    Employee         NaN         2012-01-02
3    employee           ?                  2
  • Related