# Trapped in a int -\> missing -\> float loop

**URL:** <https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578>\
**Category:** General Usage\
**Created:** [December 12, 2022, 11:14pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578 "2022-12-12T23:14:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 12, 2022, 11:14pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/1 "2022-12-12T23:14:32Z")

</div>

Let’s say that I have a dataframe like

```julia
 df = DataFrame(a=[1, 4, missing], b= [1, 4, 5], c=[1., 2, 3])
3×3 DataFrame
 Row │ a b c
     │ Int64? Int64 Float64
─────┼─────────────────────────
   1 │ 1 1 1.0
   2 │ 4 4 2.0
   3 │ missing 5 3.0

```

and want to extract all data as matrix of doubles, but can’t.

```julia
Matrix{Float64}(df)
ERROR: ArgumentError: cannot convert a DataFrame containing missing values to Matrix{Float64} (found for column a)

```

The problem seems to be related to fact that one cannot (or better, I cannot find a way to)

```julia
x = [1, 2, missing];

```

```julia
x[3] = NaN
ERROR: InexactError: Int64(NaN)

```

```julia
 Float64.(x)
ERROR: MethodError: no method matching Float64(::Missing)

```

So, we cant replace the `missing` by a float because the vector is of Integer type and can’t convert to float because of the `missing`

Any way out of this trap (other that making column copies and loop-with-ifs my way out of this)?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 12, 2022, 11:19pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/2 "2022-12-12T23:19:02Z")

</div>

`Matrix(df)` is not working?

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 12, 2022, 11:24pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/3 "2022-12-12T23:24:44Z")

</div>

Sorry, forgot a detail. I need to get read of the `missing`’s and `Matrix(df)` keeps them

```julia
Matrix(df)
3×3 Matrix{Union{Missing, Float64}}:
 1.0 1.0 1.0
 4.0 4.0 2.0
  missing 5.0 3.0

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 12, 2022, 11:25pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/4 "2022-12-12T23:25:59Z")

</div>

Use `coalesce`.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 12, 2022, 11:30pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/5 "2022-12-12T23:30:46Z")

</div>

Hmm, how?

```julia
coalesce(Matrix(df), missing)
3×3 Matrix{Union{Missing, Float64}}:
 1.0 1.0 1.0
 4.0 4.0 2.0
  missing 5.0 3.0

```

and again sorry, not yet full info. I need to replace the `missing`’s by NaN because result is intended to be sent to C lib.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 12, 2022, 11:34pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/6 "2022-12-12T23:34:06Z")

</div>

OK, contrieved but I can do

```julia
replace!(Matrix(df), missing => NaN)
3×3 Matrix{Union{Missing, Float64}}:
   1.0 1.0 1.0
   4.0 4.0 2.0
 NaN 5.0 3.0

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 12, 2022, 11:34pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/7 "2022-12-12T23:34:19Z")

</div>

Try  
`coalesce.(df, NaN)`

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 12, 2022, 11:35pm UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/8 "2022-12-12T23:35:51Z")

</div>

Thanks, that’s better as the result is directly a simple plain matrix

```julia
Matrix(coalesce.(df, NaN))
3×3 Matrix{Float64}:
   1.0 1.0 1.0
   4.0 4.0 2.0
 NaN 5.0 3.0

```

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [December 13, 2022, 6:05am UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/9 "2022-12-13T06:05:02Z")

</div>

This is why I’ve said in that “missing” is an integer.

Whereas NaN is often used as the missing for floating point.

They represent the same concept but don’t interoperate.

I don’t know if it would be useful or harmful to define conversions like:

Float64(missing) == NaN  
(and similar for NaN64, NaN32, NaN16)

Int64(NaN) == missing  
(and similar for Int32, Int16)

isnan(missing) == true

ismissing(NaN) == true

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 13, 2022, 7:53am UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/10 "2022-12-13T07:53:19Z")

</div>

Interesting idea. I started this comment disagreeing with you, but as I was typing I realized I was wrong.

`NaN` and `missing` are indeed the same concept.

`NaN` is the result of `0/0` and `Inf/Inf`. In real life we’d solve such problems using L’Hôpital’s rule and get the numeric value, but the computer doesn’t always get to do this so the numeric value is unknown.

`NaN` is a misnomer: _not a number_. But it actually _is_ a number; just a number whose value we don’t know.

Which is exactly what `missing` means when in numeric contexts.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 13, 2022, 10:40am UTC](https://discourse.julialang.org/t/trapped-in-a-int-missing-float-loop/91578/11 "2022-12-13T10:40:33Z")

</div>

Probably not helpful rehashing this here, but there’s been a lot of discussion on this when missing first came around and since then, and the reason is exists is precisely because it is meant to be distinct from NaN (and nothing). Here’s an old SO question:

> <https://stackoverflow.com/questions/61936371/usage-and-convention-differences-between-missing-nothing-undef-and-nan-in-jul>

But if you look around you’ll certainly find loads more.
