Skip to content

Commit

Permalink
BUG: Ticket #808: Insert was not performing properly when an integer was
Browse files Browse the repository at this point in the history
the argument passed to be used as the item to be insterted, and a list was
passed as the positions. This was fixed by simply duplicating the item to
be inserted so that it was a list of equal length and then control was
passed to the already exsisting code to handel this case
  • Loading branch information
lpellis authored and ericfode committed Jul 15, 2012
1 parent 143fb18 commit 2c04244
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
20 changes: 7 additions & 13 deletions numpy/lib/function_base.py
Expand Up @@ -3519,24 +3519,18 @@ def insert(arr, obj, values, axis=None):
N = arr.shape[axis]
newshape = list(arr.shape)
if isinstance(obj, (int, long, integer)):

if (obj < 0): obj += N
if obj < 0 or obj > N:
raise ValueError(
"index (%d) out of range (0<=index<=%d) "\
"in dimension %d" % (obj, N, axis))
newshape[axis] += 1;
new = empty(newshape, arr.dtype, arr.flags.fnc)
slobj[axis] = slice(None, obj)
new[slobj] = arr[slobj]
slobj[axis] = obj
new[slobj] = values
slobj[axis] = slice(obj+1,None)
slobj2 = [slice(None)]*ndim
slobj2[axis] = slice(obj,None)
new[slobj] = arr[slobj2]
if wrap:
return wrap(new)
return new

if isinstance(values, (int, long, integer)):
obj = [obj]
else:
obj = [obj] * len(values)


elif isinstance(obj, slice):
# turn it into a range object
Expand Down
3 changes: 2 additions & 1 deletion numpy/lib/tests/test_function_base.py
Expand Up @@ -145,7 +145,8 @@ def test_basic(self):
assert_equal(insert(a, 0, 1), [1, 1, 2, 3])
assert_equal(insert(a, 3, 1), [1, 2, 3, 1])
assert_equal(insert(a, [1, 1, 1], [1, 2, 3]), [1, 1, 2, 3, 2, 3])

assert_equal(insert(a, 1,[1,2,3]), [1, 1, 2, 3, 2, 3])
assert_equal(insert(a,[1,2,3],9),[1,9,2,9,3,9])

class TestAmax(TestCase):
def test_basic(self):
Expand Down

0 comments on commit 2c04244

Please sign in to comment.