Skip to content

deprecate scalar conversions for rank>0 arrays #10404

Closed
@nschloe

Description

@nschloe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    07 - Deprecation62 - Python APIChanges or additions to the Python API. Mailing list should usually be notified.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions