Pandas DataFrame Replace NaT with None
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
Make the column type as str first
dfTest2.InvoiceDate = dfTest2.InvoiceDate.astype(str)
then compare it directly with "NaT" and replace with None
dfTest2.InvoiceDate = dfTest2.InvoiceDate.apply(lambda x : None if x=="NaT" else x)
The simplest solution I found that worked for me is...
Input:
import pandas as pd
import numpy as np
dfTest = pd.DataFrame(dict(InvoiceDate=pd.to_datetime(['2017-06-01', pd.NaT]), CorpId=[2997373, np.nan], TestName=[1,1]))
dfTest.replace({np.nan: None}, inplace = True)
Output of dfTest: