Prepare an npy_index_object from the python slicing object.
This function handles all index preparations with the exception of field access. It fills the array of index_info structs correctly. It already handles the boolean array special case for fancy indexing, i.e. if the index type is boolean, it is exactly one matching boolean array. If the index type is fancy, the boolean array is already converted to integer arrays. There is (as before) no checking of the boolean dimension.
Checks everything but the bounds.
@param the array being indexed @param the index object @param index info struct being filled (size of NPY_MAXDIMS * 2 + 1) @param number of indices found @param dimension of the indexing result @param dimension of the fancy/advanced indices part @param whether to allow the boolean special case
@returns the index_type or -1 on failure and fills the number of indices.
The index might be a multi-dimensional index, but not yet a tuple this makes it a tuple in that case.
TODO: Refactor into its own function.
Next three are just to avoid slow checks
Sequences < NPY_MAXDIMS with any slice objects or newaxis, Ellipsis or other arrays or sequences embedded, are considered equivalent to an indexing tuple. (<cite>a[[[1,2], [3,4]]] == a[[1,2], [3,4]]</cite>)
If it is already a tuple, make it an exact tuple anyway
if getitem fails (unusual) treat this as a single index
We want to interpret it as a tuple, so make it one
If the index is not a tuple, handle it the same as (index,)
Parse all indices into the <cite>indices</cite> array of index_info structs
Check for single index. obj is already set then.
only one loop
** Try the cascade of possible indices
***
System Message: WARNING/2 (
<string>
, line 1);
backlink Inline strong start-string without end-string.
System Message: WARNING/2 (
<string>
, line 1);
backlink Inline emphasis start-string without end-string.
Index is an ellipsis (<cite>...</cite>)
At most one ellipsis in an index
number of slices it is worth, won't update if it is 0:
the used and new ndim will be found later
Index is np.newaxis/None
Index is a slice object.
Special case to allow 0-d boolean indexing with scalars. Should be removed after boolean as integer deprecation. Since this is always an error if it was not a boolean, we can allow the 0-d special case before the rest.
Single integer index, there are two cases here. It could be an array, a 0-d array is handled a bit weird however, so need to special case it.
At this point, we must have an index array (or array-like). It might still be a (purely) bool special case, a 0-d integer array (an array scalar) or something invalid.
TODO: Should maybe replace the error here?
For example an empty list can be cast to an integer array, however it will default to a float one.
Check if the array is valid and fill the information
There are two types of boolean indices (which are equivalent, for the most part though). A single boolean index of matching dimensionality and size is a boolean index. If this is not the case, it is instead expanded into (multiple) integer array indices.
If ndim and size match, this can be optimized as a single boolean index. The size check is necessary only to support old non-matching sizes by using fancy indexing instead. The reason for that is that fancy indexing uses nonzero, and only the result of nonzero is checked for legality.
keep track anyway, just to be complete
- TODO, WARNING: This code block cannot be used due to
- FutureWarnings at this time. So instead just raise an IndexError.
This can actually be well defined. A new axis is added, but at the same time no axis is "used". So if we have True, we add a new axis (a bit like with np.newaxis). If it is False, we add a new axis, but this axis has 0 entries.
TODO: This can't fail, right? Is there a faster way?
Convert the boolean array into multiple integer ones
Check that we will not run out of indices to store new ones
Add the arrays from the nonzero result to the index
All added indices have 1 dimension
Normal case of an integer array
A 0-d integer array is an array scalar and can be dealt with the HAS_SCALAR_ARRAY flag. We could handle 0-d arrays early on, but this makes sure that array-likes or odder arrays are always handled right.
The array does not have a valid type.
The input was an array already
The input was not an array, so give a general error message
Compare dimension of the index to the real ndim. this is to find the ellipsis value or append an ellipsis if necessary.
There is no ellipsis yet, but it is not a full index so we append an ellipsis to the end.
0-d index into 0-d array, i.e. array[()] We consider this an integer index. Which means it will return the scalar. This makes sense, because then array[...] gives an array and array[()] gives the scalar.
HAS_SCALAR_ARRAY requires cleaning up the index_type
clear as info is unnecessary and makes life harder later
A full integer index sees array scalars as part of itself
At this point indices are all set correctly, no bounds checking has been made and the new array may still have more dimensions than is possible and boolean indexing arrays may have an incorrect shape.
Check this now so we do not have to worry about it later. It can happen for fancy indexing or with newaxis. This means broadcasting errors in the case of too many dimensions take less priority.
If we had a fancy index, we may have had a boolean array index. So check if this had the correct shape now that we can find out which axes it acts on.