Skip to content

Commit

Permalink
BUF: Fix np.insert to handle boolean arrays as masks and remove Futur…
Browse files Browse the repository at this point in the history
…eWarning 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
  • Loading branch information
nocoding03 authored Oct 22, 2024
1 parent 393de0a commit e6e2948
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
22 changes: 8 additions & 14 deletions numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5380,11 +5380,13 @@ def insert(arr, obj, values, axis=None):
----------
arr : array_like
Input array.
obj : int, slice or sequence of ints
obj : slice, int, array-like of ints or bools
Object that defines the index or indices before which `values` is
inserted.
.. versionadded:: 1.8.0
.. versionchanged:: 2.1.2
Boolean indices are now treated as a mask of elements to insert,
rather than being cast to the integers 0 and 1.
Support for multiple insertions when `obj` is a single scalar or a
sequence with one element (similar to calling insert multiple
Expand Down Expand Up @@ -5491,18 +5493,10 @@ def insert(arr, obj, values, axis=None):
# need to copy obj, because indices will be changed in-place
indices = np.array(obj)
if indices.dtype == bool:
# See also delete
# 2012-10-11, NumPy 1.8
warnings.warn(
"in the future insert will treat boolean arrays and "
"array-likes as a boolean index instead of casting it to "
"integer", FutureWarning, stacklevel=2)
indices = indices.astype(intp)
# Code after warning period:
#if obj.ndim != 1:
# raise ValueError('boolean array argument obj to insert '
# 'must be one dimensional')
#indices = np.flatnonzero(obj)
if obj.ndim != 1:
raise ValueError('boolean array argument obj to insert '
'must be one dimensional')
indices = np.flatnonzero(obj)
elif indices.ndim > 1:
raise ValueError(
"index array argument obj to insert must be one dimensional "
Expand Down
10 changes: 3 additions & 7 deletions numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,9 @@ def test_basic(self):
b = np.array([0, 1], dtype=np.float64)
assert_equal(insert(b, 0, b[0]), [0., 0., 1.])
assert_equal(insert(b, [], []), b)
# Bools will be treated differently in the future:
# assert_equal(insert(a, np.array([True]*4), 9), [9, 1, 9, 2, 9, 3, 9])
with warnings.catch_warnings(record=True) as w:
warnings.filterwarnings('always', '', FutureWarning)
assert_equal(
insert(a, np.array([True] * 4), 9), [1, 9, 9, 9, 9, 2, 3])
assert_(w[0].category is FutureWarning)
assert_equal(insert(a, np.array([True]*4), 9), [9, 1, 9, 2, 9, 3, 9])
assert_equal(insert(a, np.array([True, False, True, False]), 9),
[9, 1, 2, 9, 3])

def test_multidim(self):
a = [[1, 1, 1]]
Expand Down

0 comments on commit e6e2948

Please sign in to comment.