I am trying to convert NaN (dtype: float64) values in a Pandas DataFrame column into NaT values.
Please note that I have several DataFrames with the same Order_date column. Some Order_date columns' dtypes are float64 (filled with NaN) while others' dtypes are datetime64[ns] (filled with NaT).
I tried the following:
df.loc[:,'Order_date'] = df.loc[:,'Order_date'].astype(np.datetime64).fillna(pd.NaT)
However, I get an Error result:
TypeError: cannot astype a datatimelike from [datetime64[ns]] to [datetime64].
What is reason behind this error? I think the error is due to how several Order_date columns in some DataFrames have NaT values (datetime64[ns]). What can I do to successfully convert only the Order_date columns with NaN values into NaT values in Pandas and leave the remaining Order_date columns that already have NaT values as is?