JuliaPy / PyCall.jl

Package to call Python functions from the Julia language
MIT License
1.45k stars 186 forks source link

PyObject array 'only size-1 arrays can be converted to Python scalars' #984

Closed francesco123212 closed 2 years ago

francesco123212 commented 2 years ago

Hi, hope you all are healthy. Sorry for the basic question, I am working with ansys.mapdl.core and with the function mapdl.mesh.nodes I get a pyObject array of the form:

example = 
PyObject array([[ 2.12132034e-01,  2.12132034e-01,  2.12132034e-01],
       [ 2.28079876e-01,  1.94883478e-01,  2.28079876e-01],
       .
       .
       .
       [-1.38362452e-01,  2.45949820e-01,  2.52261847e-01]])

To convert it to a Julia array I try to use convert(Matrix{Float64}, example) and I get the following error:

PyError (ccall(#= C:\Users\frcol\.julia\packages\PyCall\7a7w0\src\conversions.jl:59 =# @pysym(:PyFloat_AsDouble), Cdouble, (PyPtr,), po)) <class 'TypeError'>
TypeError('only size-1 arrays can be converted to Python scalars')

I guess it is very similar to the issue https://github.com/JuliaPy/PyCall.jl/issues/99. I also tried the following:

julia> o1 = PyObject(rand(2,3))
PyObject array([[0.87548089, 0.45301964, 0.18828053],
       [0.96245816, 0.29647598, 0.42078386]])

julia> convert(Matrix{Float64}, o1)
2×3 Matrix{Float64}:
 0.875481  0.45302   0.188281
 0.962458  0.296476  0.420784

and it works fine. Could you please tell me what I do wrong? Thanks

stevengj commented 2 years ago

Probably ansys.mapdl.core is not returning a numpy array, but instead just a list of lists.

What happens if you just do convert(PyAny, example) to let PyCall decide how to convert it?

francesco123212 commented 2 years ago

it works thankyou!