From 926564c195d30542312123b7d76fe091c3453881 Mon Sep 17 00:00:00 2001 From: Han Genuit Date: Fri, 7 Sep 2012 01:27:58 +0200 Subject: BUG: Fix for issues #378 and #392 This should fix the problems with numpy.insert(), where the input values were not checked for all scalar types and where values did not get inserted properly, but got duplicated by default. --- numpy/lib/function_base.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 247f16560..2b1d780d2 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -3591,19 +3591,21 @@ def insert(arr, obj, values, axis=None): slobj = [slice(None)]*ndim N = arr.shape[axis] newshape = list(arr.shape) - if isinstance(obj, (int, long, integer)): + 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)) - - if isinstance(values, (int, long, integer)): - obj = [obj] + if isscalar(values): + obj = [obj] else: - obj = [obj] * len(values) - + values = asarray(values) + if ndim > values.ndim: + obj = [obj] + else: + obj = [obj] * len(values) elif isinstance(obj, slice): # turn it into a range object -- cgit v1.2.1 From 1e6f7a7db94afc69f88ebf0b0235888dc377ce25 Mon Sep 17 00:00:00 2001 From: Han Genuit Date: Fri, 7 Sep 2012 01:57:24 +0200 Subject: TST: Add extra test for multidimensional inserts. --- numpy/lib/tests/test_function_base.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 7c1b01345..b619a3ffe 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -147,6 +147,13 @@ class TestInsert(TestCase): 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]) + def test_multidim(self): + a = [[1, 1, 1]] + r = [[2, 2, 2], + [1, 1, 1]] + assert_equal(insert(a, 0, [2, 2, 2], axis=0), r) + assert_equal(insert(a, 0, 2, axis=0), r) + assert_equal(insert(a, 2, 2, axis=1), [[1, 1, 2, 1]]) class TestAmax(TestCase): def test_basic(self): -- cgit v1.2.1 From 1688b29fb1ea4e548762ae79522c50abce88d55b Mon Sep 17 00:00:00 2001 From: Han Genuit Date: Fri, 7 Sep 2012 09:06:38 +0200 Subject: TST: Add test for boolean insert --- numpy/lib/tests/test_function_base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index b619a3ffe..da3eb2b84 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -147,6 +147,8 @@ class TestInsert(TestCase): 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]) + b = np.array([0, 1], dtype=np.float64) + assert_equal(insert(b, 0, b[0]), [0., 0., 1.]) def test_multidim(self): a = [[1, 1, 1]] r = [[2, 2, 2], -- cgit v1.2.1