Skip to content

Unsafe type conversion in numpy.bincount when passing list instead of array #3138

@jni

Description

@jni

np.bincount is supposed to raise a TypeError if the input array-like contains elements of float or complex type. However, this is not the observed behavior when the input is an array-like (in this case, a list), rather than an array.

>>> import numpy as np
>>> x = [1.0, 2.0, 3.5, 3.7]
>>> np.bincount(x)
array([0, 1, 1, 2])
>>> np.bincount(np.array(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot cast array data from dtype('float64') to dtype('int64') according to the rule 'safe'
>>> np.__version__
'1.7.0'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions