fillna() downcast dictionary use

900 views
Skip to first unread message

GV

unread,
Dec 4, 2014, 2:32:17 PM12/4/14
to pyd...@googlegroups.com
Hi nice people, 

For whatever reason I'm converting NaN to 0's, and need to downcast all elements in a Pandas dataframe. The fillna() method is very handy to do this, but I can't figure how can I actually make it work.

Let's say I have this dataframe:

df.dtypes 

018dcc90-b1a2-400a-a96e-64b6a101f009      int32
22d8b18e-d980-4833-ac5c-3a81c5675c8f    float64
2ca0b577-5e8e-4dc1-8ea6-600468e725b5    float64
3648db01-b29d-4ab9-835c-83f6a5068fe4    float64
83d91898-7763-47d7-b03b-b92132375c47      int32
92756526-af59-461a-9664-8d8a45da5e7b    float64
e0173ac9-387d-4e8a-a521-3e0f4cc20150    float64
e8e8538f-859b-49e1-b4fe-fefb478e355e    float64
dtype: object

The float64 dtypes come from NaN values. I want these to become a 0 and make their dtype as int32, and don't use 'infer'. According to the fillna() documentation, I did:

df.fillna(0, downcast=int)

but 

ValueError: downcast must have a dictionary or 'infer' as its argument

was returned, And so I tried

df.fillna(0, inplace=True, downcast={int:'int32'})

but got

AssertionError: dtypes as dict is not supported yet


Any help is very appreciated.

Ivan Ogasawara

unread,
Dec 4, 2014, 3:50:11 PM12/4/14
to pyd...@googlegroups.com
Hi Gabriel,

According the message, at this moment, downcast param doesn't accept a param as dict.

You can see that on the code here:

 File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 354, in downcast


Using downcast='infer', you can get a dtype=int64.


You also can change your column data type:


df = pd.DataFrame({'a': [0,1,2,3, None]}).fillna(0)

df.a = df.a.astype(np.int32)


You also  can infer the data type on the data frame creation:


df = pd.DataFrame({'a': [0,1,2,3, None]}, dtype=np.int).fillna(0)


And your dtype will be int64.


Maybe this can help you,


My best regards,


Ivan Ogasawara



--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages