2

I have created a dataframe, where the column 'bedtimes' has a datetime.datetime value or NaT if there is no date available for that index position.

I have to put this dataframe into a pymongo database. But pymongo doesn't support the NaT type but it does support nan. How can I change Nat to nan?

I already tried changing np.nan into np.NaN or change the entire column from datetime to a string or numeric but it doesn't change the NaT to Nan

        else:
            if isinstance(time, datetime.datetime):
                df.at[idx,'intended_bedtime']=time
    else:
        return np.nan

here it returns np.nan as NaT if there is no datetime row and it returns the found time if there is.

I need it to return NaN instead of NaT in order for me to put it in a Pymongo database. Not Nat to None, but NaT to nan.

1 Answers1

1

Convert column to object and then use Series.where:

df['Date'] = df['Date'].astype(object).where(df['Date'].notnull(),np.nan)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252