Insert values along the given axis before the given indices. Parameters ---------- arr : array_like Input array. obj : int, slice or sequence of ints Object that defines the index or indices before which `values` is inserted. .. versionadded:: 1
(arr, obj, values, axis=None)
| 4429 | |
| 4430 | @array_function_dispatch(_insert_dispatcher) |
| 4431 | def insert(arr, obj, values, axis=None): |
| 4432 | """ |
| 4433 | Insert values along the given axis before the given indices. |
| 4434 | |
| 4435 | Parameters |
| 4436 | ---------- |
| 4437 | arr : array_like |
| 4438 | Input array. |
| 4439 | obj : int, slice or sequence of ints |
| 4440 | Object that defines the index or indices before which `values` is |
| 4441 | inserted. |
| 4442 | |
| 4443 | .. versionadded:: 1.8.0 |
| 4444 | |
| 4445 | Support for multiple insertions when `obj` is a single scalar or a |
| 4446 | sequence with one element (similar to calling insert multiple |
| 4447 | times). |
| 4448 | values : array_like |
| 4449 | Values to insert into `arr`. If the type of `values` is different |
| 4450 | from that of `arr`, `values` is converted to the type of `arr`. |
| 4451 | `values` should be shaped so that ``arr[...,obj,...] = values`` |
| 4452 | is legal. |
| 4453 | axis : int, optional |
| 4454 | Axis along which to insert `values`. If `axis` is None then `arr` |
| 4455 | is flattened first. |
| 4456 | |
| 4457 | Returns |
| 4458 | ------- |
| 4459 | out : ndarray |
| 4460 | A copy of `arr` with `values` inserted. Note that `insert` |
| 4461 | does not occur in-place: a new array is returned. If |
| 4462 | `axis` is None, `out` is a flattened array. |
| 4463 | |
| 4464 | See Also |
| 4465 | -------- |
| 4466 | append : Append elements at the end of an array. |
| 4467 | concatenate : Join a sequence of arrays along an existing axis. |
| 4468 | delete : Delete elements from an array. |
| 4469 | |
| 4470 | Notes |
| 4471 | ----- |
| 4472 | Note that for higher dimensional inserts `obj=0` behaves very different |
| 4473 | from `obj=[0]` just like `arr[:,0,:] = values` is different from |
| 4474 | `arr[:,[0],:] = values`. |
| 4475 | |
| 4476 | Examples |
| 4477 | -------- |
| 4478 | >>> a = np.array([[1, 1], [2, 2], [3, 3]]) |
| 4479 | >>> a |
| 4480 | array([[1, 1], |
| 4481 | [2, 2], |
| 4482 | [3, 3]]) |
| 4483 | >>> np.insert(a, 1, 5) |
| 4484 | array([1, 5, 1, 2, 2, 3, 3]) |
| 4485 | >>> np.insert(a, 1, 5, axis=1) |
| 4486 | array([[1, 5, 1], |
| 4487 | [2, 5, 2], |
| 4488 | [3, 5, 3]]) |
no test coverage detected