Skip to content

Commit f0e37e8

Browse files
committed
DEP: Make np.insert and np.delete on 0d arrays with an axis an error
Before this change, the following code worked: ``` >>> some_0d = np.array(1) >>> np.insert(some_0d, "some nonsense", 10, axis=0) array(10) >>> np.insert(some_0d, "some nonsense", 42, axis="some nonsense") array(42) ``` Now these raise AxisError and TypeError, respectively. `delete` is exactly the same.
1 parent 90e644e commit f0e37e8

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
`numpy.insert` and `numpy.delete` can no longer be passed an axis on 0d arrays
2+
------------------------------------------------------------------------------
3+
This concludes a deprecation from 1.9, where when an ``axis`` argument was
4+
passed to a call to `~numpy.insert` and `~numpy.delete` on a 0d array, the
5+
``axis`` and ``obj`` argument and indices would be completely ignored.
6+
In these cases, ``insert(arr, "nonsense", 42, axis=0)`` would actually overwrite the
7+
entire array, while ``delete(arr, "nonsense", axis=0)`` would be ``arr.copy()``
8+
9+
Now passing ``axis`` on a 0d array raises `~numpy.AxisError`.

numpy/lib/function_base.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,15 +4273,6 @@ def delete(arr, obj, axis=None):
42734273
# needed for np.matrix, which is still not 1d after being ravelled
42744274
ndim = arr.ndim
42754275
axis = ndim - 1
4276-
elif ndim == 0:
4277-
# 2013-09-24, 1.9
4278-
warnings.warn(
4279-
"in the future the special handling of scalars will be removed "
4280-
"from delete and raise an error", DeprecationWarning, stacklevel=3)
4281-
if wrap:
4282-
return wrap(arr)
4283-
else:
4284-
return arr.copy(order=arrorder)
42854276
else:
42864277
axis = normalize_axis_index(axis, ndim)
42874278

@@ -4515,17 +4506,6 @@ def insert(arr, obj, values, axis=None):
45154506
# needed for np.matrix, which is still not 1d after being ravelled
45164507
ndim = arr.ndim
45174508
axis = ndim - 1
4518-
elif ndim == 0:
4519-
# 2013-09-24, 1.9
4520-
warnings.warn(
4521-
"in the future the special handling of scalars will be removed "
4522-
"from insert and raise an error", DeprecationWarning, stacklevel=3)
4523-
arr = arr.copy(order=arrorder)
4524-
arr[...] = values
4525-
if wrap:
4526-
return wrap(arr)
4527-
else:
4528-
return arr
45294509
else:
45304510
axis = normalize_axis_index(axis, ndim)
45314511
slobj = [slice(None)]*ndim

numpy/lib/tests/test_function_base.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,11 @@ def test_multidim(self):
509509
insert(a, 1, a[:, 2, :], axis=1))
510510

511511
def test_0d(self):
512-
# This is an error in the future
513512
a = np.array(1)
514-
with warnings.catch_warnings(record=True) as w:
515-
warnings.filterwarnings('always', '', DeprecationWarning)
516-
assert_equal(insert(a, [], 2, axis=0), np.array(2))
517-
assert_(w[0].category is DeprecationWarning)
513+
with pytest.raises(np.AxisError):
514+
insert(a, [], 2, axis=0)
515+
with pytest.raises(TypeError):
516+
insert(a, [], 2, axis="nonsense")
518517

519518
def test_subclass(self):
520519
class SubClass(np.ndarray):
@@ -843,10 +842,10 @@ def test_single(self):
843842

844843
def test_0d(self):
845844
a = np.array(1)
846-
with warnings.catch_warnings(record=True) as w:
847-
warnings.filterwarnings('always', '', DeprecationWarning)
848-
assert_equal(delete(a, [], axis=0), a)
849-
assert_(w[0].category is DeprecationWarning)
845+
with pytest.raises(np.AxisError):
846+
delete(a, [], axis=0)
847+
with pytest.raises(TypeError):
848+
delete(a, [], axis="nonsense")
850849

851850
def test_subclass(self):
852851
class SubClass(np.ndarray):

0 commit comments

Comments
 (0)