Closed
Description
Numpy allows for conversion of arrays into scalars if they are size-1, i.e.,
float(numpy.array(1.0)) # 1.0
float(numpy.array([1.0])) # 1.0
float(numpy.array([[[[1.0]]]])) # 1.0
# TypeError: only size-1 arrays can be converted to Python scalars
float(numpy.array([1.0, 1.0]))
I'm fine with rank-0 conversions, but I found that discrimination of conversion based on array size can easily lead to inconsistencies downstream. See, for example, sympy/sympy#13924 where suddenly you have a different behavior of multiplication depending on the length of the input array.
Moreover, this feature is actually redundant for rank>0 arrays since the value can simply be retrieved from the array via x[0]
.
When grepping for "converted to Python scalar"
in the numpy sources, one finds that numpy/lib/user_array.py
has
"only rank-0 arrays can be converted to Python scalars."
which seems to be the more consistent solution.
I would hence like to suggest to deprecate scalar conversion for rank>0 size-1 arrays.