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

**URL:** <https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063>\
**Category:** New to Julia\
**Tags:** error-message, pythoncall\
**Created:** [July 15, 2024, 5:42pm UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063 "2024-07-15T17:42:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 15, 2024, 5:42pm UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/1 "2024-07-15T17:42:53Z")

</div>

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

```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 👇

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

```

The values contained in _ **brs** _ are

```julia
[[ 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]]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 15, 2024, 5:57pm UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/2 "2024-07-15T17:57:14Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 15, 2024, 6:23pm UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/3 "2024-07-15T18:23:56Z")

</div>

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](https://discourse.julialang.org/t/error-loaderror-python-indexerror-only-integers-slices-ellipsis-numpy-newaxis-none-and-integer-or-boolean-arrays-are-valid-indices/117028/5).

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

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 16, 2024, 3:47am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/4 "2024-07-16T03:47:40Z")

</div>

> [@mbauman](#):
>
> What package does the `Py` type come from?

`Py` is coming from `broadcast.jl` package.

```julia
 [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.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 16, 2024, 7:46am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/5 "2024-07-16T07:46:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 16, 2024, 9:43am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/6 "2024-07-16T09:43:17Z")

</div>

> [@raman\_kumar](#):
>
> `Py` is coming from `broadcast.jl` package.

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.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 16, 2024, 11:03am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/7 "2024-07-16T11:03:08Z")

</div>

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

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

```

error output is shown below:

```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:99
in expression starting at /home/raman/Fluxes.jl:99

```

@cjdoris @stevengj

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 16, 2024, 4:04pm UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/8 "2024-07-16T16:04:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 18, 2024, 8:50am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/9 "2024-07-18T08:50:39Z")

</div>

I tried

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

```

but i am still getting error 🥱

```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:99
in expression starting at /home/raman/Fluxes.jl:99

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 18, 2024, 9:00am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/10 "2024-07-18T09:00:49Z")

</div>

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

```julia
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:

```julia
print(typeof(br))
x = pyconvert(Any,br)
print(typeof(x))

```

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 18, 2024, 11:41am UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/11 "2024-07-18T11:41:25Z")

</div>

No, I am still getting same error.

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [July 18, 2024, 3:40pm UTC](https://discourse.julialang.org/t/error-loaderror-methoderror-no-method-matching-isnan-py/117063/12 "2024-07-18T15:40:16Z")

</div>

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](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757), which will help you to get the best help from others in this forum.
