dpnp.insert

dpnp.insert(arr, obj, values, axis=None)[source]

Insert values along the given axis before the given indices.

For full documentation refer to numpy.insert.

Parameters:
  • arr (array_like) -- Input array.

  • obj ({slice, int, array-like of ints or bools}) -- Object that defines the index or indices before which values is inserted. It supports multiple insertions when obj is a single scalar or a sequence with one element (similar to calling insert multiple times). Boolean indices are treated as a mask of elements to insert.

  • values (array_like) -- Values to insert into arr. If the type of values is different from that of arr, values is converted to the type of arr. values should be shaped so that arr[..., obj, ...] = values is legal.

  • axis ({None, int}, optional) -- Axis along which to insert values. If axis is None then arr is flattened first. Default: None.

Returns:

out -- A copy of arr with values inserted. Note that dpnp.insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

Return type:

dpnp.ndarray

See also

dpnp.append

Append elements at the end of an array.

dpnp.concatenate

Join a sequence of arrays along an existing axis.

dpnp.delete

Delete elements from an array.

Notes

Note that for higher dimensional inserts obj=0 behaves very different from obj=[0] just like arr[:, 0, :] = values is different from arr[:, [0], :] = values.

Examples

>>> import dpnp as np
>>> a = np.array([[1, 1], [2, 2], [3, 3]])
>>> a
array([[1, 1],
       [2, 2],
       [3, 3]])
>>> np.insert(a, 1, 5)
array([1, 5, 1, 2, 2, 3, 3])
>>> np.insert(a, 1, 5, axis=1)
array([[1, 5, 1],
       [2, 5, 2],
       [3, 5, 3]])

Difference between sequence and scalars:

>>> np.insert(a, [1], [[1],[2],[3]], axis=1)
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])
>>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1),
...                np.insert(a, [1], [[1],[2],[3]], axis=1))
array(True)
>>> b = a.flatten()
>>> b
array([1, 1, 2, 2, 3, 3])
>>> np.insert(b, [2, 2], [5, 6])
array([1, 1, 5, 6, 2, 2, 3, 3])
>>> np.insert(b, slice(2, 4), [5, 6])
array([1, 1, 5, 2, 6, 2, 3, 3])
>>> np.insert(b, [2, 2], [7.13, False]) # dtype casting
array([1, 1, 7, 0, 2, 2, 3, 3])
>>> x = np.arange(8).reshape(2, 4)
>>> idx = (1, 3)
>>> np.insert(x, idx, 999, axis=1)
array([[  0, 999,   1,   2, 999,   3],
       [  4, 999,   5,   6, 999,   7]])