pandasdatetimeto-datecoerce

Pandas: checking invalid values in to_datetime function


im trying to convert string into datetype using:

df.apply(pd.to_datetime)

that throws: ('month must be in 1..12', 'occurred at index 'my_column')

Adding errors='coerce' fixes problem

df.apply(pd.to_datetime, errors='coerce')

But is there any way to check and display the invalid values?


Solution

  • You can just create a new column then use boolean indexing:

    # sample data
    df = pd.DataFrame({'str':['2019-01-01', '2019-02-01', '2019-13-01']})
    # adding a new column that is datetime
    df['date'] = pd.to_datetime(df['str'], errors='coerce')
    # boolean indexing
    df[df['date'].isna()]
    
    
            str     date
    2   2019-13-01   NaT