Pandas DataFrame Replace NaT with None

前端 未结 4 862
南笙
南笙 2020-12-14 08:55

I have been struggling with this question for a long while, and I tried different methods.

I have a simple DataFrame as shown,

I can use code to rep

4条回答
  •  时光说笑
    2020-12-14 09:30

    Make the dtype object

    dfTest2 = pd.DataFrame(dict(InvoiceDate=pd.to_datetime(['2017-06-01', pd.NaT])))
    
    dfTest2.InvoiceDate.astype(object).where(dfTest2.InvoiceDate.notnull(), None)
    
    0    2017-06-01 00:00:00
    1                   None
    Name: InvoiceDate, dtype: object
    

提交回复
热议问题