np.insert return positional error
ampangboy opened this issue · 3 comments
ampangboy commented
When attempting to use np.insert
function, it will be return an error as below
Python.Runtime.PythonException: 'TypeError : insert() missing 1 required positional argument: 'obj''
The method is called as below:
var arr = np.array(new double[1]{ 1 });
var valueToInsert = np.array(new double[1]{ 1 });
np.insert(npArr, 0, valueToInsert);
henon commented
ok, I tried this on the python console to get a reference what should be the result of your code
>>> import numpy as np
>>> arr = np.array([1])
>>> arr
array([1])
>>> value = np.array([1])
>>> result = np.insert(arr, 0, value)
>>> result
array([1, 1])
>>>
I'll fix np.insert now
henon commented
I fixed np.insert and your code can be simplified to this:
[TestMethod]
public void IssueByAmpangboy()
{
var arr = np.array(1.0);
var result=np.insert(arr, 0, 1.0);
Assert.AreEqual("array([1., 1.])", result.repr);
}
I also added all overloads of np.insert allowing all kinds of combinations of numbers, arrays, slices as parameters:
[TestMethod]
public void insertTest()
{
// >>> a = np.array([[1, 1], [2, 2], [3, 3]])
// >>> a
// array([[1, 1],
// [2, 2],
// [3, 3]])
// >>> np.insert(a, 1, 5)
// array([1, 5, 1, 2, 2, 3, 3])
// >>> np.insert(a, 1, 5, axis=1)
// array([[1, 5, 1],
// [2, 5, 2],
// [3, 5, 3]])
//
NDarray a = np.array(new[,] { { 1, 1 }, { 2, 2 }, { 3, 3 } });
var given = a;
var expected =
"array([[1, 1],\n" +
" [2, 2],\n" +
" [3, 3]])";
Assert.AreEqual(expected, given.repr);
given = np.insert(a, 1, 5);
expected =
"array([1, 5, 1, 2, 2, 3, 3])";
Assert.AreEqual(expected, given.repr);
given = np.insert(a, 1, 5, axis: 1);
expected =
"array([[1, 5, 1],\n" +
" [2, 5, 2],\n" +
" [3, 5, 3]])";
Assert.AreEqual(expected, given.repr);
// Difference between sequence and scalars:
// >>> np.insert(a, [1], [[1],[2],[3]], axis=1)
// array([[1, 1, 1],
// [2, 2, 2],
// [3, 3, 3]])
given = np.insert(a, np.array(1), np.array(new[,] { { 1 }, { 2 }, { 3 } }), axis: 1);
expected =
"array([[1, 1, 1],\n" +
" [2, 2, 2],\n" +
" [3, 3, 3]])";
// >>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1),
// ... np.insert(a, [1], [[1],[2],[3]], axis=1))
// True
//
Assert.AreEqual(expected, given.repr);
var equal = np.array_equal(np.insert(a, 1, np.array(new[] { 1, 2, 3 }), axis: 1),
np.insert(a, np.array(1), np.array(new[,] { { 1 }, { 2 }, { 3 } }), axis: 1));
Assert.AreEqual(true, equal);
// >>> b = a.flatten()
// >>> b
// array([1, 1, 2, 2, 3, 3])
// >>> np.insert(b, [2, 2], [5, 6])
// array([1, 1, 5, 6, 2, 2, 3, 3])
//
var b = a.flatten();
given = b;
expected =
"array([1, 1, 2, 2, 3, 3])";
Assert.AreEqual(expected, given.repr);
given = np.insert(b, np.array(new[] { 2, 2 }), np.array(new[] { 5, 6 }));
expected =
"array([1, 1, 5, 6, 2, 2, 3, 3])";
Assert.AreEqual(expected, given.repr);
// >>> np.insert(b, slice(2, 4), [5, 6])
// array([1, 1, 5, 2, 6, 2, 3, 3])
//
given= np.insert(b, new Slice(2, 4), np.array(new[] { 5, 6 }));
expected=
"array([1, 1, 5, 2, 6, 2, 3, 3])";
Assert.AreEqual(expected, given.repr);
// >>> np.insert(b, [2, 2], [7.13, False]) # type casting
// array([1, 1, 7, 0, 2, 2, 3, 3])
//
#if NOT_SUPPORTED
given = np.insert(b, np.array(new[] { 2, 2 }), np.array(new object[] { 7.13, false })); // type casting
expected=
"array([1, 1, 7, 0, 2, 2, 3, 3])";
Assert.AreEqual(expected, given.repr);
#endif
// >>> x = np.arange(8).reshape(2, 4)
// >>> idx = (1, 3)
// >>> np.insert(x, idx, 999, axis=1)
// array([[ 0, 999, 1, 2, 999, 3],
// [ 4, 999, 5, 6, 999, 7]])
//
var x = np.arange(8).reshape(2, 4);
NDarray idx = np.array(1, 3);
given= np.insert(x, idx, np.array(999), axis:1);
expected=
"array([[ 0, 999, 1, 2, 999, 3],\n" +
" [ 4, 999, 5, 6, 999, 7]])";
Assert.AreEqual(expected, given.repr);
}
henon commented
Nuget is updated shortly