Search code examples
pythonpandasdatetimedata-conversion

Skipping non-date time values when converting to datetime in Pandas


I have a dataframe where one of the columns has data that I want to turn into datetime. But there are other, miscellaneous values in that column. Do I need to clean out those values before I convert? I keep getting an error about these rouge values gumming up my pd.to_datetime function.

Is there a way I can 'skip' over non-date values and convert only date values to datetime?


Solution

  • You can use the errors='coerce' option, e.g.:

    df = pd.DataFrame({'date': '2021-01-01', 'foo', '2021-03-31'})
    
    #          date
    # 0  2021-01-01
    # 1         foo
    # 2  2021-03-31
    
    df.date = pd.to_datetime(df.date, errors='coerce')
    
    #          date
    # 0  2021-01-01
    # 1         NaT
    # 2  2021-03-31