Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NaT to blank in pandas dataframe

Tags:

python

pandas

I have a dataframe (df) that looks like:

        DATES
0         NaT
1  01/08/2003
2         NaT
3         NaT
4  04/08/2003
5         NaT
6  30/06/2003
7  01/03/2004
8  18/05/2003
9         NaT
10        NaT
11 31/10/2003
12        NaT
13        NaT

I am struggling to find out how I transform the data-frame to remove the NaT values so the final output looks like

        DATES
0         
1  01/08/2003
2         
3         
4  04/08/2003
5         
6  30/06/2003
7  01/03/2004
8  18/05/2003
9         
10        
11 31/10/2003
12        
13  

I have tried :

df["DATES"].fillna("", inplace = True)

but with no success.

For information the column is in a datatime format set with

df["DATES"] = pd.to_datetime(df["DATES"],errors='coerce').dt.strftime('%d/%m/%Y')

What can I do to resolve this?

like image 887
Stacey Avatar asked Jan 25 '23 19:01

Stacey


2 Answers

df.fillna() works on numpy.NaN values. Your "Nat" are probably strings. So you can do following,

if you want to use fillna()

df["DATES"].replace("NaT",np.NaN, inplace=True)
df.fillna("", inplace=True)

Else, you can just replace with your desired string

df["DATES"].replace("NaT","", inplace=True)
like image 42
UserOnWeb Avatar answered Jan 30 '23 20:01

UserOnWeb


There is problem NaT are strings, so need:

df["DATES"] = df["DATES"].replace('NaT', '')
like image 166
jezrael Avatar answered Jan 30 '23 22:01

jezrael