Skip to content

Commit e6e2948

Browse files
authored
BUF: Fix np.insert to handle boolean arrays as masks and remove FutureWarning and change the corresponding test (numpy#27615)
* Fix np.insert to correctly handle boolean arrays as masks * Fix np.insert to correctly handle boolean arrays as masks * Fix np.insert to correctly handle boolean arrays as masks and change the Corresponding test
1 parent 393de0a commit e6e2948

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

numpy/lib/_function_base_impl.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5380,11 +5380,13 @@ def insert(arr, obj, values, axis=None):
53805380
----------
53815381
arr : array_like
53825382
Input array.
5383-
obj : int, slice or sequence of ints
5383+
obj : slice, int, array-like of ints or bools
53845384
Object that defines the index or indices before which `values` is
53855385
inserted.
53865386
5387-
.. versionadded:: 1.8.0
5387+
.. versionchanged:: 2.1.2
5388+
Boolean indices are now treated as a mask of elements to insert,
5389+
rather than being cast to the integers 0 and 1.
53885390
53895391
Support for multiple insertions when `obj` is a single scalar or a
53905392
sequence with one element (similar to calling insert multiple
@@ -5491,18 +5493,10 @@ def insert(arr, obj, values, axis=None):
54915493
# need to copy obj, because indices will be changed in-place
54925494
indices = np.array(obj)
54935495
if indices.dtype == bool:
5494-
# See also delete
5495-
# 2012-10-11, NumPy 1.8
5496-
warnings.warn(
5497-
"in the future insert will treat boolean arrays and "
5498-
"array-likes as a boolean index instead of casting it to "
5499-
"integer", FutureWarning, stacklevel=2)
5500-
indices = indices.astype(intp)
5501-
# Code after warning period:
5502-
#if obj.ndim != 1:
5503-
# raise ValueError('boolean array argument obj to insert '
5504-
# 'must be one dimensional')
5505-
#indices = np.flatnonzero(obj)
5496+
if obj.ndim != 1:
5497+
raise ValueError('boolean array argument obj to insert '
5498+
'must be one dimensional')
5499+
indices = np.flatnonzero(obj)
55065500
elif indices.ndim > 1:
55075501
raise ValueError(
55085502
"index array argument obj to insert must be one dimensional "

numpy/lib/tests/test_function_base.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,9 @@ def test_basic(self):
557557
b = np.array([0, 1], dtype=np.float64)
558558
assert_equal(insert(b, 0, b[0]), [0., 0., 1.])
559559
assert_equal(insert(b, [], []), b)
560-
# Bools will be treated differently in the future:
561-
# assert_equal(insert(a, np.array([True]*4), 9), [9, 1, 9, 2, 9, 3, 9])
562-
with warnings.catch_warnings(record=True) as w:
563-
warnings.filterwarnings('always', '', FutureWarning)
564-
assert_equal(
565-
insert(a, np.array([True] * 4), 9), [1, 9, 9, 9, 9, 2, 3])
566-
assert_(w[0].category is FutureWarning)
560+
assert_equal(insert(a, np.array([True]*4), 9), [9, 1, 9, 2, 9, 3, 9])
561+
assert_equal(insert(a, np.array([True, False, True, False]), 9),
562+
[9, 1, 2, 9, 3])
567563

568564
def test_multidim(self):
569565
a = [[1, 1, 1]]

0 commit comments

Comments
 (0)