Guide to NumPy Indexing¶
This section of the ndindex documentation discusses the semantics of NumPy
indices. This really is more of a documentation of NumPy itself than of
ndindex. However, understanding the underlying semantics of indices is
critical to making the best use of ndindex, as well as for making the best use
of NumPy arrays themselves. Furthermore, the sections on integer
indices and slices also apply to the built-in
Python sequence types like list
and str
.
This guide is aimed for people who are new to NumPy indexing semantics, but it also tries to be as complete as possible and at least mention all the various corner cases. Some of these technical points can be glossed over if you are a beginner.
Table of Contents¶
This guide is split into four sections.
After a short introduction, the first two sections cover the basic
single-axis index types: integer indices, and
slices. These are the indices that only work on a single axis of
an array at a time. These are also the indices that work on built-in sequence
types such as list
and str
. The semantics of these index types on list
and str
are exactly the same as on NumPy arrays, so even if you do not care
about NumPy or array programming, these sections of this document can be
informative just as a general Python programmer. Slices in particular are oft
confused and the guide on slicing clarifies their exact rules and debunks some
commonly spouted false beliefs about how they work.
The third section covers multidimensional
indices. These indices will not work on
the built-in Python sequence types like list
and str
; they are only
defined for NumPy arrays. This section is itself split into six subsections.
First is a basic introduction to what a NumPy array
is. Following this are pages
for each of the remaining index types, the basic indices:
tuples,
ellipses, and
newaxis; and the advanced indices:
integer arrays and boolean
arrays (i.e., masks).
Finally, a page on other topics relevant to indexing covers a set of miscellaneous topics about NumPy arrays that are useful for understanding how indexing works, such as broadcasting, views, strides, and ordering.