# Using \`isnan()\` with missing values leads to hard to find bugs

**URL:** https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399
**Category:** General Usage
**Created:** [April 11, 2020, 3:31pm UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399 "2020-04-11T15:31:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [April 11, 2020, 3:31pm UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/1 "2020-04-11T15:31:46Z")

</div>

I found some curious behavior when using the `isnan()`function on a dataframe with missing values the other day.

I was trying to replace NaN values with missing values in the dataframe. However, I found that when  
I encountered `isnan(missing)` that this evaluated to `missing` instead of True or False. This was generating errors in the code that were hard to find, since the error message was related to the if-else function instead of the isnan() function.

I was just wondering if this was expected behavior. Seems a bit odd that a boolean function would return a non-boolean value like missing. But that could just be my simple mind’s way of thinking of things.

Anyhow, just wondering if this is really an issue I should submit, or if this is expected behavior? Thanks.

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [April 11, 2020, 3:50pm UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/2 "2020-04-11T15:50:50Z")

</div>

This is expected behavior. The value in question is `missing`, so we don’t know what the value is. It could be `NaN`, since we can’t know for sure, `isnan` returns missing.

Can i ask what language you are coming from? Different languages have different approaches to missing values.

---

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [April 11, 2020, 10:08pm UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/3 "2020-04-11T22:08:14Z")

</div>

@pdeffebach Okay cool, good to know. I am coming from Python and R background. It just caught me by surprise that a boolean would return a missing value, instead of True or False. But that is okay, now I know to check for missing values before using the `isnan()` or other similar functions.

---

<div class="post-metadata">

### Author: ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)
#### Post date: [April 11, 2020, 10:24pm UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/4 "2020-04-11T22:24:28Z")

</div>

You could do something like this

```julia
isnanmissing(x::Union{Number,Missing}) = ismissing(x) ? true : isnan(x)

```

which returns true if the number is missing or NaN

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [April 11, 2020, 10:30pm UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/5 "2020-04-11T22:30:06Z")

</div>

Yes it’s a bit more strict than R.

In R you use `na.rm = T` everywhere. The equivalent usage would be to pass `skipmissing(x)` to all your functions. Another solution is to do `replace`

```julia
julia> replace(x, NaN => 0, missing => 100)

```

Check out [Missing.jl](https://github.com/JuliaData/Missings.jl) for convenience functions.

---

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [April 12, 2020, 1:34am UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/6 "2020-04-12T01:34:15Z")

</div>

Oh yes, that makes a lot more sense. I forgot that `skipmissing()` existed, so I can use that. Sounds good, thanks for setting me straight.

---

<div class="post-metadata">

### Author: ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)
#### Post date: [April 12, 2020, 1:35am UTC](https://discourse.julialang.org/t/using-isnan-with-missing-values-leads-to-hard-to-find-bugs/37399/7 "2020-04-12T01:35:53Z")

</div>

Oh great, yes this will work. Nice, I will add this function to my little code library for future use. I am slowly starting to get a handle on the julia idioms. Thanks again.
