Skip to content

Inconsistent behavior of ufuncs with 0d arrays #4563

@SylvainCorlay

Description

@SylvainCorlay

0d arrays are a special case for ufuncs. Indeed, rather than returning a 0d array, ufuncs return scalars in case of a 0d array inputs.
For example

print np.exp(np.array(1.0)).__class__

returns <type 'numpy.float64'>, while

print np.exp(np.array([1.0, 2.0])).__class__

returns <type 'numpy.ndarray'>.

Ideally ufuncs should return 0d arrays in case of 0d array inputs, and scalars in case of scalar inputs. (array>scalar) returns an array in every cases except in the case of a 0d array, which in my opinion breaks the Zen of Python.

One of the consequences of the behavior was that x[x>0] was giving an error in the case of 0d-arrays, which was solved by PR #3798 by allowing scalar boolean to be used as an index for 0d array.

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