diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2013-03-07 19:09:31 +0100 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-04-11 18:52:03 +0200 |
commit | 6d305e49c155b744a699e9d09ca9132ee663018a (patch) | |
tree | 838612f6eeec8a95712a5938271e9f9e23026208 /numpy/lib | |
parent | f17e55ddefe0e3c730be8220476b1a7ae8dc7a43 (diff) | |
download | numpy-6d305e49c155b744a699e9d09ca9132ee663018a.tar.gz |
MAINT: np.delete keep old out of bound/negative index behavior
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 32 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 23 |
2 files changed, 36 insertions, 19 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 81cfecb9d..d67b97b1d 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3386,7 +3386,7 @@ def meshgrid(*xi, **kwargs): def delete(arr, obj, axis=None): """ 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,]`. + dimensional array, this returns those entries not returned by `arr[obj]`. Parameters ---------- @@ -3453,9 +3453,9 @@ def delete(arr, obj, axis=None): ndim = arr.ndim; axis = ndim-1; if ndim == 0: - warnings.warn("in the future the special handleing of scalars " + warnings.warn("in the future the special handling of scalars " "will be removed from delete and raise an error", - FutureWarning) + DeprecationWarning) if wrap: return wrap(arr) else: @@ -3517,15 +3517,15 @@ def delete(arr, obj, axis=None): _obj = obj obj = np.asarray(obj) - # After removing the special handling of booleans, the size == 1 check - # can be just replaced with something else (like integer check) - # and there need not be any conversion to an array at all. + # After removing the special handling of booleans and out of + # bounds values, the conversion to the array can be removed. if obj.dtype == bool: warnings.warn("in the future insert will treat boolean arrays " "and array-likes as boolean index instead " "of casting it to integer", FutureWarning) obj = obj.astype(intp) - if obj.size == 1: + if isinstance(_obj, (int, long, integer)): + # optimization for a single value obj = obj.item() if (obj < -N or obj >=N): raise IndexError("index %i is out of bounds for axis " @@ -3549,6 +3549,20 @@ def delete(arr, obj, axis=None): "will result in an error in the future", DeprecationWarning) obj = obj.astype(intp) keep = ones(N, dtype=bool) + + # Test if there are out of bound indices, this is deprecated + inside_bounds = (obj < N) & (obj >= -N) + if not inside_bounds.all(): + warnings.warn("in the future out of bounds indices will raise an " + "error instead of being ignored by `numpy.delete`.", + DeprecationWarning) + obj = obj[inside_bounds] + positive_indices = obj >= 0 + if not positive_indices.all(): + warnings.warn("in the future negative indices will not be ignored " + "by `numpy.delete`.", FutureWarning) + obj = obj[positive_indices] + keep[obj,] = False slobj[axis] = keep new = arr[slobj] @@ -3660,9 +3674,9 @@ def insert(arr, obj, values, axis=None): ndim = arr.ndim axis = ndim-1 if (ndim == 0): - warnings.warn("in the future the special handleing of scalars " + warnings.warn("in the future the special handling of scalars " "will be removed from insert and raise an error", - FutureWarning) + DeprecationWarning) arr = arr.copy() arr[...] = values if wrap: diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 06d202bbb..ae68be41f 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -214,9 +214,9 @@ class TestInsert(TestCase): # This is an error in the future a = np.array(1) with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', '', FutureWarning) + warnings.filterwarnings('always', '', DeprecationWarning) assert_equal(insert(a, [], 2, axis=0), np.array(2)) - assert_(w[0].category is FutureWarning) + assert_(w[0].category is DeprecationWarning) def test_subclass(self): class SubClass(np.ndarray): @@ -357,14 +357,15 @@ class TestDelete(TestCase): def _check_inverse_of_slicing(self, indices): a_del = delete(self.a, indices) nd_a_del = delete(self.nd_a, indices, axis=1) + msg = 'Delete failed for obj: %r' % indices # NOTE: The cast should be removed after warning phase for bools - if not isinstance(indices, slice): + if not isinstance(indices, (slice, int, long, np.integer)): indices = np.asarray(indices, dtype=np.intp) + indices = indices[(indices >= 0) & (indices < 5)] assert_array_equal(setxor1d(a_del, self.a[indices,]), self.a, - err_msg='Delete failed for obj: %r' % indices) + err_msg=msg) xor = setxor1d(nd_a_del[0,:,0], self.nd_a[0,indices,0]) - assert_array_equal(xor, self.nd_a[0,:,0], - err_msg='Delete failed for obj: %r' % indices) + assert_array_equal(xor, self.nd_a[0,:,0], err_msg=msg) def test_slices(self): lims = [-6, -2, 0, 1, 2, 4, 5] @@ -376,11 +377,13 @@ class TestDelete(TestCase): self._check_inverse_of_slicing(s) def test_fancy(self): - self._check_inverse_of_slicing([0, -1, 2, 2]) + # Deprecation/FutureWarning tests should be kept after change. self._check_inverse_of_slicing(np.array([[0,1],[2,1]])) - assert_raises(IndexError, delete, self.a, [100]) + assert_raises(DeprecationWarning, delete, self.a, [100]) + assert_raises(DeprecationWarning, delete, self.a, [-100]) with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', FutureWarning) + self._check_inverse_of_slicing([0, -1, 2, 2]) obj = np.array([True, False, False], dtype=bool) self._check_inverse_of_slicing(obj) assert_(w[0].category is FutureWarning) @@ -393,9 +396,9 @@ class TestDelete(TestCase): def test_0d(self): a = np.array(1) with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', '', FutureWarning) + warnings.filterwarnings('always', '', DeprecationWarning) assert_equal(delete(a, [], axis=0), a) - assert_(w[0].category is FutureWarning) + assert_(w[0].category is DeprecationWarning) def test_subclass(self): class SubClass(np.ndarray): |