How to pass array to a function in .net using Pythonnet?

1

I am using a library in Python called trendln

I am trying to call it through C# form. For that I have used Pythonnet Python.Runtime.Dll. My Python version is 3.7.8.

In python, it was easy to fetch the function using:

mins, maxs = trendln.calc_support_resistance(h)

where h = hist[-1000:].Close.

Similarly, I thought of trying it through C# like this:

using (Py.GIL())
{
    trendln = Py.Import("trendln");                
    double[] h = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
    int a, b = trendln.calc_support_resistance(h);
}

But I am getting the following error:

Python.Runtime.PythonException
  HResult=0x80131500
  Message=ValueError : h is not list, numpy ndarray or pandas Series of numeric values or a 2-tuple thereof
  Source=Python.Runtime
  StackTrace:
['  File "C:\\Python37\\lib\\site-packages\\trendln\\__init__.py", line 486, in calc_support_resistance\n    else: raise ValueError(\'h is not list, numpy ndarray or pandas Series of numeric values or a 2-tuple thereof\')\n']   at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw)
   at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw)
   at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at testapplication.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\test\Desktop\C#\testapplication\testapplication\Form1.cs:line 31
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at testapplication.Program.Main() in C:\Users\test\Desktop\C#\testapplication\testapplication\Program.cs:line 19

Please, let me know what I can do to avoid this error. How I can pass and receive the right information with type?

python
c#
python-3.x
python.net
asked on Stack Overflow Nov 24, 2020 by Jaffer Wilson

2 Answers

1

This worked for me:

using (Py.GIL())
{
    dynamic h = np.arange(1000);
    PyTuple b = new PyTuple(trendln.calc_support_resistance(h));
    
    Console.WriteLine(b.ToString());
}
answered on Stack Overflow Nov 24, 2020 by Jaffer Wilson
0

I suppose, h variable cannot be initialized as a list.

Try initializing it like this;

using (Py.GIL())
{
    trendln = Py.Import("trendln");
    dynamic h = new float[] { 1F, 2F, 3F };
    int a, b = trendln.calc_support_resistance(h);
}
answered on Stack Overflow Nov 24, 2020 by tozlu • edited Nov 24, 2020 by tozlu

User contributions licensed under CC BY-SA 3.0