Repository: pythonnet/pythonnet

Issue #1887: Cannot convert Numpy Float64 array to double[]
Created: 2022-07-25T13:00:25Z
Environment

Pythonnet version: 3.0.0-rc4
Python version: 3.7 (also tested with 3.10)
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 correctly
using (Py.GIL())
{
    PyModule scope = Py.CreateScope();
    scope.Exec(
        "import numpy as np \n" +
        "arr = np.array([1., 2.])"
    );
    PyObject arr = scope.Get("arr");
    Type dataType = typeof(double[]);
    var array = 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!
Cheers,
Lukas

Duplicate issue #1276: Decoder for primitive types in ctypes module
Created: 2020-11-09T21:37:44Z
ctypes is a standard Python module for interacting with fixed size integers. Would be nice to provide C# <-> Python conversions for types in that module.
Relevant: https://stackoverflow.com/questions/63801150/how-do-i-pass-a-ulong-type-argument-to-a-c-sharp-function-from-python

Closing comment (2022-07-29T16:33:31Z): A realted topic is numpy and pandas types (any instance of np.ndarray, and pd.core.arrays.ExtensionArray. I tried the following but it didn't work - it's not even triggered, I guess because it's primitives?
    public class NumpyDecoder : IPyObjectDecoder
    {
        public bool CanDecode(PyType objectType, Type targetType)
        {
            return targetType == typeof(double) && objectType.Name.StartsWith("numpy.float64");
        }

        public bool TryDecode<T>(PyObject pyObj, out T value)
        {
            value = (T)(object)pyObj.GetAttr("item").As<double>();
            return true;
        }
    }
Python.Runtime.PyObjectConversions.RegisterDecoder(NumpyDecoder())
xs = np.array([1.0, 2.0], dtype=np.float64)
lst = List[Double]()
lst.Add(xs[0])        # Fails
lst.Add(xs[0].item()) # Works
This is also reported in #1887
See also https://stackoverflow.com/questions/9452775/converting-numpy-dtypes-to-native-python-types