P.J P.J - 29 days ago 9
Python Question

Is there any way to delete the specific elements of an numpy array "In-place" in python:

when calling the "np.delete()", I am not interested to define a new variable for the reduced size array. I want to execute the delete on the original numpy array. Any thought?

>>> arr = np.array([[1,2], [5,6], [9,10]])
>>> arr
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
>>> np.delete(arr, 1, 0)
array([[ 1, 2],
[ 9, 10]])
>>> arr
array([[ 1, 2],
[ 5, 6],
[ 9, 10]])
but I want:
>>> arr
array([[ 1, 2],
[ 9, 10]])

Answer

NumPy arrays are fixed-size, so there can't be an in-place version of np.delete. Any such function would have to change the array's size.

The closest you can get is reassigning the arr variable:

arr = numpy.delete(arr, 1, 0)
Comments