ERROR: LoadError: MethodError: no method matching isnan(::Py)

I am getting error while running a local python library in julia.

ERROR: LoadError: MethodError: no method matching isnan(::Py)

Closest candidates are:
  isnan(::BigFloat)
   @ Base mpfr.jl:982
  isnan(::Missing)
   @ Base missing.jl:101
  isnan(::Complex)
   @ Base complex.jl:151
  ...

Stacktrace:
 [1] _broadcast_getindex_evalf
   @ ./broadcast.jl:709 [inlined]
 [2] _broadcast_getindex
   @ ./broadcast.jl:682 [inlined]
 [3] getindex
   @ ./broadcast.jl:636 [inlined]
 [4] copy
   @ ./broadcast.jl:918 [inlined]
 [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(isnan), Tuple{Base.RefValue{Py}}})
   @ Base.Broadcast ./broadcast.jl:903
 [6] top-level scope
   @ ~/Fluxes.jl:96
in expression starting at /home/raman/Fluxes.jl:96

Subpart of my code is :point_down:

brh = br[0, pyslice(nothing)] 
print(brh)
where_are_nan = isnan.(brh)
brh[where_are_nan] .= 0

The values contained in brs are

[[            nan             nan             nan ... -8.23414666e-04
  -5.94470800e-04 -3.51605759e-04]
 [            nan             nan             nan ... -4.46161745e-04
  -2.93301076e-04 -1.83216718e-04]
 [            nan             nan             nan ... -2.04372700e-04
  -1.43195490e-04 -9.47578434e-05]
 ...
 [            nan             nan             nan ... -1.50316128e-05
  -7.53571822e-05 -1.21192903e-04]
 [            nan             nan             nan ...  4.58974279e-05
  -4.22424439e-05 -8.68374244e-05]
 [            nan             nan             nan ...  1.64187791e-04
  -1.50739786e-05 -7.63359852e-05]]

I suspect your brh contains some kind of Python object representing an array, which isnan doesn’t know what to do with. Either you need to use some Python function for isnan on your object, or you need to convert it into a Julia Array so you can process it with Julia functions.

(No, I don’t know the details of either option, but conversion should be described in the PythonCall documentation, if that’s what you use to call Python.)

1 Like

What package does the Py type come from? That’s probably the most important thing here — and it’d be good for you to understand why that’s the important thing.

You’re shuttling things back and forth between the two languages with one of the python interop packages. Thinking about just this one issue in isolation will likely just get you to the next section of code — which is exactly what happened with your previous question.

Instead of looking at these errors one-by-one, I’d encourage you to try to holistically dig into the conversion rules between the two languages in PythonCall.jl (I think that’s what you’re using) — and ask questions about the grand scheme!

3 Likes

Py is coming from broadcast.jl package.

 [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(isnan), Tuple{Base.RefValue{Py}}})
   @ Base.Broadcast ./broadcast.jl:903

I don’t know how to convert Py to julia type.

As others have said, you should have a read through the PythonCall docs, in particular the pyconvert and PyArray functions.

Bit of a tangent here, but

  1. that’s not a package, .jl is just a file extension for Julia source code.
  2. The stacktrace snippet says plainly that the materialize method is part of Base, a base library that comes with Julia, not a package
  3. Nowhere in that method signature suggests that the Py type comes from Base or its submodule Broadcast, and it does not. Use parentmodule for this instead.
2 Likes

I tried using pyconvert(Array,br) but it still gives Py type.

B = pyfiles.metric.TensorDotProduct(ecov, b)
br = (B[1, 0])
pyconvert(PyArray,br)
print(typeof(br))

error output is shown below:

ERROR: LoadError: MethodError: no method matching isnan(::Py)

Closest candidates are:
  isnan(::BigFloat)
   @ Base mpfr.jl:982
  isnan(::Missing)
   @ Base missing.jl:101
  isnan(::Complex)
   @ Base complex.jl:151
  ...

Stacktrace:
 [1] _broadcast_getindex_evalf
   @ ./broadcast.jl:709 [inlined]
 [2] _broadcast_getindex
   @ ./broadcast.jl:682 [inlined]
 [3] getindex
   @ ./broadcast.jl:636 [inlined]
 [4] copy
   @ ./broadcast.jl:918 [inlined]
 [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(isnan), Tuple{Base.RefValue{Py}}})
   @ Base.Broadcast ./broadcast.jl:903
 [6] top-level scope
   @ ~/Fluxes.jl:99
in expression starting at /home/raman/Fluxes.jl:99

@cjdoris @stevengj

Can you show your code and output? It’s difficult to help otherwise.

I tried

br = (B[1, 0])
pyconvert(Any,br)
print(typeof(br))
print(ispy(br))

but i am still getting error :yawning_face:

ERROR: LoadError: MethodError: no method matching isnan(::Py)

Closest candidates are:
  isnan(::BigFloat)
   @ Base mpfr.jl:982
  isnan(::Missing)
   @ Base missing.jl:101
  isnan(::Complex)
   @ Base complex.jl:151
  ...

Stacktrace:
 [1] _broadcast_getindex_evalf
   @ ./broadcast.jl:709 [inlined]
 [2] _broadcast_getindex
   @ ./broadcast.jl:682 [inlined]
 [3] getindex
   @ ./broadcast.jl:636 [inlined]
 [4] copy
   @ ./broadcast.jl:918 [inlined]
 [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(isnan), Tuple{Base.RefValue{Py}}})
   @ Base.Broadcast ./broadcast.jl:903
 [6] top-level scope
   @ ~/Fluxes.jl:99
in expression starting at /home/raman/Fluxes.jl:99

I don’t know the PythonCall package but if you do

print(typeof(br))
pyconvert(Any,br)
print(typeof(br))

you will most likely see that nothing has happened with the type. However, if you use the output from pyconvert you may have something that becomes more useful later on:

print(typeof(br))
x = pyconvert(Any,br)
print(typeof(x))
1 Like

No, I am still getting same error.

Please post the complete code. Your error message is about isnan but you don’t use isnan in the code you posted.

Also please read Please read: make it easier to help you, which will help you to get the best help from others in this forum.

2 Likes