Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas DataFrame Replace NaT with None

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

I have a simple DataFrame as shown,

enter image description here

I can use code to replace NaN with None (Not String "None"),

[![dfTest2 = dfTest.where(pd.notnull(dfTest), None)][2]][2]

enter image description here

I support that NaT is also classified as 'Null' because the following, enter image description here

However, NaT is not replaced with None.

I have been searching for answers but got no luck. Anyone could Help?

Thank you in advance.

like image 851
Haipeng Su Avatar asked Mar 15 '17 18:03

Haipeng Su


People also ask

How do you change NaT to blank in Python?

replace({pd. NaT: None}, inplace=True) .

Is NaT same as NaN?

Another bizarre thing about missing values in Pandas is that some varieties are equal to themselves and others aren't. NaN doesn't equal NaN . And NaT doesn't equal NaT . But None does equal None .

How do you Fillna with none?

pandas fillna NaN with None Value In order to update the existing DataFrame use df. fillna('None', inplace=True) . You can also use pandas.


4 Answers

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 
like image 73
piRSquared Avatar answered Sep 19 '22 19:09

piRSquared


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:

enter image description here

like image 39
dshefman Avatar answered Sep 21 '22 19:09

dshefman


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)
like image 36
Neeraj Yadav Avatar answered Sep 20 '22 19:09

Neeraj Yadav


Similar approach as suggested by @neerajYadav but without the apply:

dfTest2['InvoiceDate'] = (dfTest2['InvoiceDate']
                          .astype(str) # <- cast to string to simplify
                                       #    .replace() in newer versions
                          .replace({'NaT': None} # <- replace with None
                         )
like image 34
Snake Verde Avatar answered Sep 21 '22 19:09

Snake Verde