Thanks to visit codestin.com
Credit goes to github.com

Skip to content

take does not specify behavior for 0-D input array #855

Closed
@ndgrigorian

Description

@ndgrigorian

It is not clarified in the spec how take should handle 0-D inputs for the indexed (x) argument.

NumPy allows this, even when axis=0

In [1]: import numpy as np

In [2]: np.take(np.ones(()), np.zeros(2, dtype="i4"), axis=0)
Out[2]: array([1., 1.])

of course, this won't work when using Python-sequence-style indexing

In [4]: np.ones(())[np.zeros(2, dtype="i4")]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[4], line 1
----> 1 np.ones(())[np.zeros(2, dtype="i4")]

IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

Referring to take(x, indices, axis=3 as "conceptually equivalent" to x[:, :, :, indices, ...] implicitly disallows this behavior.

The description of the output seems to agree

out (array) – an array having the same data type as x. The output array must have the same rank (i.e., number of dimensions) as x and must have the same shape as x, except for the axis specified by axis whose size must equal the number of elements in indices.

This wouldn't make much sense for a 0-D array, much like in cumulative_sum a similar conclusion was drawn.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions