larray.Array.insert

Array.insert(self, value, before=None, after=None, pos=None, axis=None, label=None)[source]

Inserts value in array along an axis.

Parameters
valuescalar or Array

Value to insert. If an Array, it must have compatible axes. If value already has the axis along which it is inserted, label should not be used.

beforescalar or Group

Label or group before which to insert value.

afterscalar or Group

Label or group after which to insert value.

labelstr, optional

Label for the new item in axis.

Returns
Array

Array with value inserted along axis. The dtype of the returned array will be the “closest” type which can hold both the array values and the inserted values without loss of information. For example, when mixing numeric and string types, the dtype will be object.

Examples

>>> arr1 = ndtest((2, 3))
>>> arr1
a\b  b0  b1  b2
 a0   0   1   2
 a1   3   4   5
>>> arr1.insert(42, before='b1', label='b0.5')
a\b  b0  b0.5  b1  b2
 a0   0    42   1   2
 a1   3    42   4   5

The inserted value can be an array:

>>> arr2 = ndtest(2)
>>> arr2
a  a0  a1
    0   1
>>> arr1.insert(arr2, after='b0', label='b0.5')
a\b  b0  b0.5  b1  b2
 a0   0     0   1   2
 a1   3     1   4   5

If you want to target positions, you have to somehow specify the axis:

>>> a, b = arr1.axes
>>> # arr1.insert(42, before='b.i[1]', label='b0.5')
>>> arr1.insert(42, before=b.i[1], label='b0.5')
a\b  b0  b0.5  b1  b2
 a0   0    42   1   2
 a1   3    42   4   5

Insert an array which already has the axis

>>> arr3 = ndtest('a=a0,a1;b=b0.1,b0.2') + 42
>>> arr3
a\b  b0.1  b0.2
 a0    42    43
 a1    44    45
>>> arr1.insert(arr3, before='b1')
a\b  b0  b0.1  b0.2  b1  b2
 a0   0    42    43   1   2
 a1   3    44    45   4   5