如何使用 Pythonnet 将数组传递给 .net 中的函数?
How to pass array to a function in .net using Pythonnet?
我正在 Python 中使用名为 trendln
的库
我正在尝试通过 C# 形式调用它。为此,我使用了 Pythonnet Python.Runtime.Dll
。我的 Python 版本是 3.7.8.
在 python 中,使用以下方法很容易获取函数:
mins, maxs = trendln.calc_support_resistance(h)
其中 h = hist[-1000:].Close
.
类似地,我想通过 C# 尝试这样:
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);
}
但我收到以下错误:
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
请告诉我如何避免此错误。我如何传递和接收类型正确的信息?
我想,h
变量不能初始化为列表。
尝试像这样初始化它;
using (Py.GIL())
{
trendln = Py.Import("trendln");
dynamic h = new float[] { 1F, 2F, 3F };
int a, b = trendln.calc_support_resistance(h);
}
这对我有用:
using (Py.GIL())
{
dynamic h = np.arange(1000);
PyTuple b = new PyTuple(trendln.calc_support_resistance(h));
Console.WriteLine(b.ToString());
}
我正在 Python 中使用名为 trendln
的库我正在尝试通过 C# 形式调用它。为此,我使用了 Pythonnet Python.Runtime.Dll
。我的 Python 版本是 3.7.8.
在 python 中,使用以下方法很容易获取函数:
mins, maxs = trendln.calc_support_resistance(h)
其中 h = hist[-1000:].Close
.
类似地,我想通过 C# 尝试这样:
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);
}
但我收到以下错误:
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
请告诉我如何避免此错误。我如何传递和接收类型正确的信息?
我想,h
变量不能初始化为列表。
尝试像这样初始化它;
using (Py.GIL())
{
trendln = Py.Import("trendln");
dynamic h = new float[] { 1F, 2F, 3F };
int a, b = trendln.calc_support_resistance(h);
}
这对我有用:
using (Py.GIL())
{
dynamic h = np.arange(1000);
PyTuple b = new PyTuple(trendln.calc_support_resistance(h));
Console.WriteLine(b.ToString());
}