[Numpy-discussion] Error when using uints in bincount()

Stephen Simmons mail at stevesimmons.com
Thu Jul 20 21:05:48 CDT 2006


Hi,

The function bincount() counts the number of each value found in the 
input array:

In [15]: numpy.bincount( array([1,3,3,3,4],dtype=int32) )
Out[15]: array([0, 1, 0, 3, 1])

According to the documentation, the input array must be non-negative 
integers.

However an exception occurs when the input data type are unsigned integers
(which is an explicit guarantee of this non-negativity condition):

In [157]: numpy.bincount( array([1,3,3,3,4],dtype=uint32) )
TypeError: array cannot be safely cast to required type

This seems to be a bug.

Cheers

Stephen


P.S.  I'm not familiar enough with the numpy source to track down where 
this typechecking is done. But I did find a trivial typo in an error msg 
in function arr_bincount() in numpy/lib/src/_compiled_base.c. The assert 
message here has lost its initial 'F':

    Py_Assert(numbers[mni] >= 0, 
	      "irst argument of bincount must be non-negative");
 






More information about the Numpy-discussion mailing list