Skip to content

Commit

Permalink
Add a section on bounds checking to the integer array indices page
Browse files Browse the repository at this point in the history
  • Loading branch information
asmeurer committed May 22, 2024
1 parent db4c04a commit a5add8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/indexing-guide/integer-indices.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Therefore, negative indices are primarily a syntactic convenience that
allows one to specify parts of a list that would otherwise need to be
specified in terms of the size of the list.

(integer-indices-bounds-checking)=
If an integer index is greater than or equal to the size of the list, or less
than negative the size of the list (`i >= len(a)` or `i < -len(a)`), then it
is out of bounds and will raise an `IndexError`.
Expand Down
22 changes: 22 additions & 0 deletions docs/indexing-guide/multidimensional-indices/integer-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,28 @@ array([100, 101, 103])
array([100, 101, 103])
```

### Bounds Checking

As with [integer indices](../integer-indices.md), integer array indexing uses
bounds checking, with the [same rule as integer
indices](integer-indices-bounds-checking).

> **If any entry in an integer array index is greater than `size - 1` or less
> than `-size`, where `size` is the size of the dimension being indexed, an
> `IndexError` is raised.**
```py
>>> a = np.array([100, 101, 102, 103]) # as above
>>> a[[2, 3, 4]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 4 is out of bounds for axis 0 with size 4
>>> a[[-5, -4, -3]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index -5 is out of bounds for axis 0 with size 4
```

(integer-array-broadcasting)=
### Broadcasting

Expand Down

0 comments on commit a5add8b

Please sign in to comment.