0

I am using Python 3.5 with numpy version 1.11.3 and I am facing a really weird issue that might be difficult to reproduce.

I loaded a Numpy array arr1 from a pd.DataFrame and np.delete does not seem to work:

ipdb> np.delete(arr1, 53)
array([  53,   84,  140,  220,  295,  413,  478,  558,  596,  875,  986,
       1103, 1487, 1559, 1704, 1924, 2009, 2044, 2301, 2410, 2514, 2746,
       3432, 3443, 3466, 4054, 4125, 4249, 4309, 4395, 4429, 4544, 4764,
       4787, 5208, 5299, 5340, 5447, 5680, 5899, 5977, 6254, 6256, 6276,
       6412, 6518, 6538, 6584, 6587, 6591, 6592, 6593, 6594, 6661, 6662,
       6663, 6664, 6665, 6666, 6667, 6668, 6669, 6670, 6671, 6672, 6673,
       6686, 6698, 6699, 6700, 6770, 6796, 6848, 6881, 6917, 6975, 7079,
       7121, 7188, 7402, 7510, 8200, 8217, 8479, 8569, 8759, 8925, 9152,
       9190, 9243, 9423, 9485, 9583, 9681, 9690, 9692, 9710, 9793, 9811])

ipdb> arr1.dtype
dtype('int64')

ipdb> np.delete(arr1, arr1)
array([  53,   84,  140,  220,  295,  413,  478,  558,  596,  875,  986,
       1103, 1487, 1559, 1704, 1924, 2009, 2044, 2301, 2410, 2514, 2746,
       3432, 3443, 3466, 4054, 4125, 4249, 4309, 4395, 4429, 4544, 4764,
       4787, 5208, 5299, 5340, 5447, 5680, 5899, 5977, 6254, 6256, 6276,
       6412, 6518, 6538, 6584, 6587, 6591, 6592, 6593, 6594, 6661, 6662,
       6663, 6664, 6665, 6666, 6667, 6668, 6669, 6670, 6671, 6672, 6673,
       6686, 6698, 6699, 6700, 6770, 6796, 6848, 6881, 6917, 6975, 7079,
       7121, 7188, 7402, 7510, 8200, 8217, 8479, 8569, 8759, 8925, 9152,
       9190, 9243, 9423, 9485, 9583, 9681, 9690, 9692, 9710, 9793, 9811])

However it works when I call np.delete on np.arange:

ipdb> np.delete(np.arange(15), np.arange(13))
array([13, 14])

Is there an explanation why this might happen ?

1 Answer 1

1

The second argument of numpy.delete is not the value to be deleted. It is the index (or indices) to be deleted. Take a look at the examples in the docstring, such as this one:

In [25]: arr = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])

In [26]: arr
Out[26]: 
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

In [27]: np.delete(arr, 1, axis=0)
Out[27]: 
array([[ 1,  2,  3,  4],
       [ 9, 10, 11, 12]])

The value 1 in that function call, combined with axis=0, means "delete the row with index 1".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.