8000 Merge pull request #26609 from jud-sdev/update-insert · mathomp4/numpy@09779f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 09779f9

Browse files
authored
Merge pull request numpy#26609 from jud-sdev/update-insert
DOC: Updated notes and examples for np.insert.
2 parents 7f6ac72 + 25a93a3 commit 09779f9

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

numpy/lib/_function_base_impl.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5418,43 +5418,49 @@ def insert(arr, obj, values, axis=None):
54185418
-----
54195419
Note that for higher dimensional inserts ``obj=0`` behaves very different
54205420
from ``obj=[0]`` just like ``arr[:,0,:] = values`` is different from
5421-
``arr[:,[0],:] = values``.
5421+
``arr[:,[0],:] = values``. This is because of the difference between basic
5422+
and advanced :ref:`indexing <basics.indexing>`.
54225423
54235424
Examples
54245425
--------
5425-
>>> a = np.array([[1, 1], [2, 2], [3, 3]])
5426+
>>> a = np.arange(6).reshape(3, 2)
54265427
>>> a
5427-
array([[1, 1],
5428-
[2, 2],
5429-
[3, 3]])
5430-
>>> np.insert(a, 1, 5)
5431-
array([1, 5, 1, ..., 2, 3, 3])
5432-
>>> np.insert(a, 1, 5, axis=1)
5433-
array([[1, 5, 1],
5434-
[2, 5, 2],
5435-
[3, 5, 3]])
5436-
5437-
Difference between sequence and scalars:
5438-
5439-
>>> np.insert(a, [1], [[1],[2],[3]], axis=1)
5440-
array([[1, 1, 1],
5441-
[2, 2, 2],
5442-
[3, 3, 3]])
5443-
>>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1),
5444-
... np.insert(a, [1], [[1],[2],[3]], axis=1))
5428+
array([[0, 1],
5429+
[2, 3],
5430+
[4, 5]])
5431+
>>> np.insert(a, 1, 6)
5432+
array([0, 6, 1, 2, 3, 4, 5])
5433+
>>> np.insert(a, 1, 6, axis=1)
5434+
array([[0, 6, 1],
5435+
[2, 6, 3],
5436+
[4, 6, 5]])
5437+
5438+
Difference between sequence and scalars,
5439+
showing how ``obj=[1]`` behaves different from ``obj=1``:
5440+
5441+
>>> np.insert(a, [1], [[7],[8],[9]], axis=1)
5442+
array([[0, 7, 1],
5443+
[2, 8, 3],
5444+
[4, 9, 5]])
5445+
>>> np.insert(a, 1, [[7],[8],[9]], axis=1)
5446+
array([[0, 7, 8, 9, 1],
5447+
[2, 7, 8, 9, 3],
5448+
[4, 7, 8, 9, 5]])
5449+
>>> np.array_equal(np.insert(a, 1, [7, 8, 9], axis=1),
5450+
... np.insert(a, [1], [[7],[8],[9]], axis=1))
54455451
True
54465452
54475453
>>> b = a.flatten()
54485454
>>> b
5449-
array([1, 1, 2, 2, 3, 3])
5450-
>>> np.insert(b, [2, 2], [5, 6])
5451-
array([1, 1, 5, ..., 2, 3, 3])
5455+
array([0, 1, 2, 3, 4, 5])
5456+
&g 80D7 t;>> np.insert(b, [2, 2], [6, 7])
5457+
array([0, 1, 6, 7, 2, 3, 4, 5])
54525458
5453-
>>> np.insert(b, slice(2, 4), [5, 6])
5454-
array([1, 1, 5, ..., 2, 3, 3])
5459+
>>> np.insert(b, slice(2, 4), [7, 8])
5460+
array([0, 1, 7, 2, 8, 3, 4, 5])
54555461
54565462
>>> np.insert(b, [2, 2], [7.13, False]) # type casting
5457-
array([1, 1, 7, ..., 2, 3, 3])
5463+
array([0, 1, 7, 0, 2, 3, 4, 5])
54585464
54595465
>>> x = np.arange(8).reshape(2, 4)
54605466
>>> idx = (1, 3)

0 commit comments

Comments
 (0)
0