vendredi 24 juin 2016

Multiplication of two positive numbers gives a negative output in Python 3

I have a DataFrame df1 :

df1.head() = 

             wght          num_links 
id_y  id_x                      
 3     133   0.000203          2      
       186   0.000203          2 
 5     6     0.000203          2      
       98    0.000203          2      
       184   0.000203          2

I need to calculate a variable called thr,

thr = N*(N-1)*2,

where Nis the number of rows of df1.

The problem is that when I calculate thr,Python throws a negative value(although all of the inputs are positive):

ipdb> df1['wght'].count()*(df1['wght'].count()-1)*2
-712569744 

Possible hint

The number of rows N is

ipdb> df1['wght'].count() 
137736 

therefore,

ipdb> 137736*137735*2
37942135920.

Taking into account that the max value that can be assigned to a int32 is 2147483647, I suspect that NumPy considers type(thr) = <int32>, when it should be <int64>. Does this make sense?

Please note that I have not written the code that generates df1 because

ipdb> df1['wght'].count() 
137736

However, if it is needed to reproduce the error, let me know.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire