# Error from conversion of missing values to NaNs

**URL:** <https://discourse.julialang.org/t/error-from-conversion-of-missing-values-to-nans/108321>\
**Category:** General Usage\
**Tags:** question, missing-values, netcdf\
**Created:** [January 4, 2024, 12:38am UTC](https://discourse.julialang.org/t/error-from-conversion-of-missing-values-to-nans/108321 "2024-01-04T00:38:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 4, 2024, 12:48am UTC](https://discourse.julialang.org/t/error-from-conversion-of-missing-values-to-nans/108321/2 "2024-01-04T00:48:22Z")

</div>

> [@Leon6](#):
>
> every time when A is composed of integers, it is causing errors

Because [`NaN` is exclusively a floating-point value](https://en.wikipedia.org/wiki/NaN) — you can’t store that in an integer array.

> [@Leon6](#):
>
> `InexactError: Int32(NaN)`

If you have a `Vector{Int32}`, then it is stored in memory as a sequence of consecutive 32-bit signed integer (`Int32`) values. Every possible bit pattern of an `Int32` represents some integer — there is no bit pattern that represents a `NaN` without changing the type of the elements.

What you could do is

```julia
A = float(A)
A[ismissing.(A)] .= NaN

```

to ensure that `A` is a floating-point container. Or alternatively:

```julia
Anan = map(x -> ismissing(x) ? NaN : Float64(x), A)

```

which has the advantage that the container type of `Anan` will be concretely `Vector{Float64}`, not `Vector{Union{Float64,Missing}}` — the compiler will know that it can no longer contain missing values and optimize accordingly.

PS. Please quote your code: [PSA: how to quote code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530)

---

_[View the full topic](https://discourse.julialang.org/t/error-from-conversion-of-missing-values-to-nans/108321)._
