summaryrefslogtreecommitdiff
path: root/doc/source
diff options
context:
space:
mode:
authorPierre de Buyl <pdebuyl@pdebuyl.be>2020-03-04 10:34:56 +0100
committerGitHub <noreply@github.com>2020-03-04 11:34:56 +0200
commitd5064d4ab5152c71c4b639fe581cc80b74a103e4 (patch)
tree020be12dee83a174eb7bd352fa95c6c9a0081ac1 /doc/source
parent6894bbc6d396b87464cbc21516d239d5f94f13b7 (diff)
downloadnumpy-d5064d4ab5152c71c4b639fe581cc80b74a103e4.tar.gz
DOC: Fix indexing docs to pass refguide (#15694)
* DOC: Add missing imports, np prefix, fix printing, fix examples Co-Authored-By: Eric Wieser <wieser.eric@gmail.com>
Diffstat (limited to 'doc/source')
-rw-r--r--doc/source/reference/arrays.indexing.rst33
1 files changed, 12 insertions, 21 deletions
diff --git a/doc/source/reference/arrays.indexing.rst b/doc/source/reference/arrays.indexing.rst
index 8ec8d8330..a425b6e8b 100644
--- a/doc/source/reference/arrays.indexing.rst
+++ b/doc/source/reference/arrays.indexing.rst
@@ -1,3 +1,6 @@
+.. for doctests
+ >>> import numpy as np
+
.. _arrays.indexing:
Indexing
@@ -265,10 +268,10 @@ understood with an example.
one needs to select all elements *explicitly*. Using the method explained
previously one could write:
- >>> x = array([[ 0, 1, 2],
- ... [ 3, 4, 5],
- ... [ 6, 7, 8],
- ... [ 9, 10, 11]])
+ >>> x = np.array([[ 0, 1, 2],
+ ... [ 3, 4, 5],
+ ... [ 6, 7, 8],
+ ... [ 9, 10, 11]])
>>> rows = np.array([[0, 0],
... [3, 3]], dtype=np.intp)
>>> columns = np.array([[0, 2],
@@ -392,7 +395,7 @@ smaller than *x* it is identical to filling it with :const:`False`.
>>> x = np.array([[1., 2.], [np.nan, 3.], [np.nan, np.nan]])
>>> x[~np.isnan(x)]
- array([ 1., 2., 3.])
+ array([1., 2., 3.])
Or wish to add a constant to all negative elements:
@@ -422,18 +425,6 @@ with.
array([[0, 1],
[1, 1]])
- But if ``rowsum`` would have two dimensions as well:
-
- >>> rowsum = x.sum(-1, keepdims=True)
- >>> rowsum.shape
- (3, 1)
- >>> x[rowsum <= 2, :] # fails
- IndexError: too many indices
- >>> x[rowsum <= 2]
- array([0, 1])
-
- The last one giving only the first elements because of the extra dimension.
- Compare ``rowsum.nonzero()`` to understand this example.
Combining multiple Boolean indexing arrays or a Boolean with an integer
indexing array can best be understood with the
@@ -447,10 +438,10 @@ also supports boolean arrays and will work without any surprises.
advanced integer index. Using the :func:`ix_` function this can be done
with:
- >>> x = array([[ 0, 1, 2],
- ... [ 3, 4, 5],
- ... [ 6, 7, 8],
- ... [ 9, 10, 11]])
+ >>> x = np.array([[ 0, 1, 2],
+ ... [ 3, 4, 5],
+ ... [ 6, 7, 8],
+ ... [ 9, 10, 11]])
>>> rows = (x.sum(-1) % 2) == 0
>>> rows
array([False, True, False, True])