pandas astype applied to long integer returns a truncated result

Viewed 153

wondering if someone came across this issue before I'm trying to cast a float column in a dataframe to integer , and i'm getting strange results, this is my code :

proj_id['test2'] = proj_id['campaign_id'].astype('int64')
proj_id[proj_id['campaign_id']==23847591030830034][['campaign_id','test2']]

enter image description here

so my campaign_id which was 23847591030830034 becomes 23847591030830032

i have tried to suppress scientific expression, rounding, ... but it seems like the conversion truncates a byte off my integer

thank you for your help

1 Answers

This seems like it is a matter of representation.

When you do indexing you are casting 23847591030830034 to float which then gets compared with another float that is more accurately represented as 23847591030830032.0 but it is a difference so small that it gets rounded to the same float:

>>> floated = float(23847591030830034)
>>> inted = 23847591030830032
>>> floated == float(inted)
True

So it seems that the float representation for both integers is the same hence the discrepancy.

Related