I am trying to convert a date column to datetime64[ns]
and I am using the following line
df["Date"] = df["Date"].astype('datetime64[ns]',errors="ignore")
If I don't use ignore
key, the script crashes at this line.
Now some of the values in this column are incorrect and they may nor may not be date at all. How do I convert all the date values to the date format and replace all non date values to
Edit: The coerce
doesn't work anymore as per the compiler, only options are raise
and ignore
CodePudding user response:
Use to_datetime
with errors='coerce'
and then replace missing values:
datetime = '2000-01-01'
df["Date"] = pd.to_datetime(df["Date"], errors='coerce').fillna(pd.Timestamp(datetime))
If possible missing values in column and need not convert them:
datetime = '2000-01-01'
m = df["Date"].notna()
df.loc[m, "Date"] = (pd.to_datetime(df.loc[m, "Date"], errors='coerce')
.fillna(pd.Timestamp(datetime)))