Search code examples
pythonpandasdatetimetry-exceptdate-conversion

Pandas: convert object to Date Time, removing exceptions


I am trying to convert an Object to a Date Time variable. However, some dates are outside the time range. How could I fix it with a try..except construct, removing exceptions outside time range?

df['Date_Time'] = df['Date_Time'].astype('datetime64[ns]')

Error:

ValueError: year 60521 is out of range

I tried this, but it didn't work:

try:
    df['Date_Time'] = df['Date_Time'].astype('datetime64[ns]')
except:
    pass

Sample input:

0          2020-09-08 23:59:41
1          2020-09-08 23:58:07
2          2020-09-08 23:59:42

Solution

  • use the errors parameter of the to_datetime function.

    df['Date_Time'] = pandas.to_datetime(df['Date_Time'], errors='coerce')
    

    any errors in conversion will be set as NaT