Return a new array with sub-arrays along an axis deleted. For a one dimensional array, this returns those entries not returned by `arr[obj]`. Parameters ---------- arr : array_like Input array. obj : slice, int or array of ints Indicate indices of sub-ar
(arr, obj, axis=None)
| 5172 | |
| 5173 | @array_function_dispatch(_delete_dispatcher) |
| 5174 | def delete(arr, obj, axis=None): |
| 5175 | """ |
| 5176 | Return a new array with sub-arrays along an axis deleted. For a one |
| 5177 | dimensional array, this returns those entries not returned by |
| 5178 | `arr[obj]`. |
| 5179 | |
| 5180 | Parameters |
| 5181 | ---------- |
| 5182 | arr : array_like |
| 5183 | Input array. |
| 5184 | obj : slice, int or array of ints |
| 5185 | Indicate indices of sub-arrays to remove along the specified axis. |
| 5186 | |
| 5187 | .. versionchanged:: 1.19.0 |
| 5188 | Boolean indices are now treated as a mask of elements to remove, |
| 5189 | rather than being cast to the integers 0 and 1. |
| 5190 | |
| 5191 | axis : int, optional |
| 5192 | The axis along which to delete the subarray defined by `obj`. |
| 5193 | If `axis` is None, `obj` is applied to the flattened array. |
| 5194 | |
| 5195 | Returns |
| 5196 | ------- |
| 5197 | out : ndarray |
| 5198 | A copy of `arr` with the elements specified by `obj` removed. Note |
| 5199 | that `delete` does not occur in-place. If `axis` is None, `out` is |
| 5200 | a flattened array. |
| 5201 | |
| 5202 | See Also |
| 5203 | -------- |
| 5204 | insert : Insert elements into an array. |
| 5205 | append : Append elements at the end of an array. |
| 5206 | |
| 5207 | Notes |
| 5208 | ----- |
| 5209 | Often it is preferable to use a boolean mask. For example: |
| 5210 | |
| 5211 | >>> arr = np.arange(12) + 1 |
| 5212 | >>> mask = np.ones(len(arr), dtype=bool) |
| 5213 | >>> mask[[0,2,4]] = False |
| 5214 | >>> result = arr[mask,...] |
| 5215 | |
| 5216 | Is equivalent to ``np.delete(arr, [0,2,4], axis=0)``, but allows further |
| 5217 | use of `mask`. |
| 5218 | |
| 5219 | Examples |
| 5220 | -------- |
| 5221 | >>> arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]]) |
| 5222 | >>> arr |
| 5223 | array([[ 1, 2, 3, 4], |
| 5224 | [ 5, 6, 7, 8], |
| 5225 | [ 9, 10, 11, 12]]) |
| 5226 | >>> np.delete(arr, 1, 0) |
| 5227 | array([[ 1, 2, 3, 4], |
| 5228 | [ 9, 10, 11, 12]]) |
| 5229 | |
| 5230 | >>> np.delete(arr, np.s_[::2], 1) |
| 5231 | array([[ 2, 4], |