summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2014-08-30 09:25:52 +0100
committerSebastian Berg <sebastian@sipsolutions.net>2014-08-30 17:57:31 +0100
commitf880b1aa7583d7c3dfc111a8b79e7e7ba364baf2 (patch)
tree0b4f8c6f238ac664df782e54cd2f414ff611f28b /numpy/lib
parent709a06d0e9db862a8dd519db13724a4c59de7d69 (diff)
downloadnumpy-f880b1aa7583d7c3dfc111a8b79e7e7ba364baf2.tar.gz
BUG: Fix np.insert for inserting a single item into a structured array
Note that there are some object array special cases because of allowing multiple inserts. `np.array(..., dtype=object)` is not always clear.
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/function_base.py4
-rw-r--r--numpy/lib/tests/test_function_base.py12
2 files changed, 14 insertions, 2 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 087d1cbb5..ff69b07dc 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -3758,7 +3758,9 @@ def insert(arr, obj, values, axis=None):
if (index < 0):
index += N
- values = array(values, copy=False, ndmin=arr.ndim)
+ # There are some object array corner cases here, but we cannot avoid
+ # that:
+ values = array(values, copy=False, ndmin=arr.ndim, dtype=arr.dtype)
if indices.ndim == 0:
# broadcasting is very different here, since a[:,0,:] = ... behaves
# very different from a[:,[0],:] = ...! This changes values so that
diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py
index f70d7dda0..6bc622f23 100644
--- a/numpy/lib/tests/test_function_base.py
+++ b/numpy/lib/tests/test_function_base.py
@@ -310,6 +310,16 @@ class TestInsert(TestCase):
np.insert([0, 1, 2], x, [3, 4, 5])
assert_equal(x, np.array([1, 1, 1]))
+ def test_structured_array(self):
+ a = np.array([(1, 'a'), (2, 'b'), (3, 'c')],
+ dtype=[('foo', 'i'), ('bar', 'a1')])
+ val = (4, 'd')
+ b = np.insert(a, 0, val)
+ assert_array_equal(b[0], np.array(val, dtype=b.dtype))
+ val = [(4, 'd')] * 2
+ b = np.insert(a, [0, 2], val)
+ assert_array_equal(b[[0, 3]], np.array(val, dtype=b.dtype))
+
class TestAmax(TestCase):
def test_basic(self):
@@ -1458,7 +1468,7 @@ class TestMeshgrid(TestCase):
# Test that meshgrid complains about invalid arguments
# Regression test for issue #4755:
# https://github.com/numpy/numpy/issues/4755
- assert_raises(TypeError, meshgrid,
+ assert_raises(TypeError, meshgrid,
[1, 2, 3], [4, 5, 6, 7], indices='ij')