Operating System: Windows 10 Enterprise (10.0.19044 Build 19044)
.NET Runtime: .NET 6.0
Details
Hi,
We want to update our PythonEnvironment (conda env) to 3.10 (previously were using 3.7). Therefore I'm trying to update the our used pythonnet version to the latest prerelease one. (3.0.0-rc4 right now) However, when doing so, our unit tests for converting 1, 2 and 3 dimensional numpy (float) arrays to a CLR (double[] type) are breaking. I debugged it and could nail it down to be reproducible using the following code (which executes using version 3.0.0-preview2021-08-03):
// call PythonEngine.Initialize() and set PythonHome and Paths correctlyusing(Py.GIL()){PyModulescope=Py.CreateScope();scope.Exec("import numpy as np \n"+"arr = np.array([1., 2.])");PyObjectarr=scope.Get("arr");TypedataType=typeof(double[]);vararray=arr.AsManagedObject(dataType);// CollectionAssert.AreEqual(new double[] { 1.0, 2.0 }, (double[])result);}
This throws an InvalidCastException in AsManagedObject:
System.InvalidCastException: cannot convert object to target type
---> Python.Runtime.PythonException: 'numpy.float64' value cannot be converted to System.Double
Let me know if you need something else or if I made a mistake. We were initially using pythonnet version 2.5.2 but I tried to refactor and change all the code as required for 3.0.0.
Help is highly appreciated. I couldn't find something/someone else that is having problems converting numpy arrays. Which seems a bit weird to me.
Thanks for your efforts developing this great and helpful library, keep it up!
Hi @filmor,
Thanks for your guidance. I've added the pandasnet code from @fdieulle to my project and I can convert numpy arrays and pandas dataframes once again.
However, for the conversion of a numpy.float64 I keep ending up in a conversion error within pythonnet.
I did this and then called my conversion function:
Internally in the NumpyCodecs.cs it's then mapped to the pythonnet function PyObject.As<double>:
codecs.Register("<class 'numpy.float64'>", p => p.As<double>());
but when this is executed, I'm getting the mentioned conversion exception within pythonnet.
Refer to my screenshot below for the excact locations and stack trace.
I'm not sure how this is supposed to work. I'm also not sure if I should report this issue in here or in the pandasnet github repo tbh.
Any help is highly appreciated. It should be also easy to reproduce.
Thanks
Activity
filmor commented on Jul 25, 2022
We don't include non-trivial conversions by default anymore. You should use a codec like this for the conversion: https://github.com/fdieulle/pandasnet/blob/main/dotnet/PandasNet/NumpyCodec.cs
Tarudro commented on Jul 28, 2022
Hi @filmor,
Thanks for your guidance. I've added the pandasnet code from @fdieulle to my project and I can convert numpy arrays and pandas dataframes once again.
However, for the conversion of a numpy.float64 I keep ending up in a conversion error within pythonnet.
I did this and then called my conversion function:
PyModule scope = Py.CreateScope(); scope.Exec( "import numpy as np \n" + "float64_var = np.float64(1.2)" ); PyObject float64_var = scope.Get("float64_var");Internally in the NumpyCodecs.cs it's then mapped to the pythonnet function PyObject.As<double>:
codecs.Register("<class 'numpy.float64'>", p => p.As<double>());but when this is executed, I'm getting the mentioned conversion exception within pythonnet.

Refer to my screenshot below for the excact locations and stack trace.
I'm not sure how this is supposed to work. I'm also not sure if I should report this issue in here or in the pandasnet github repo tbh.
Any help is highly appreciated. It should be also easy to reproduce.
Thanks