# Efficient way to transform Array{Union{Missing,Float64}} to Array{Float64}?

**URL:** <https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475>\
**Category:** General Usage\
**Created:** [March 28, 2019, 7:22pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475 "2019-03-28T19:22:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 28, 2019, 7:22pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/1 "2019-03-28T19:22:27Z")

</div>

there is a way to transform an array `x1` of Union{Missing,Float64} to an Array `x2` of Float64 without allocating a new array? .`x1` doesn’t containg `missing` values. an example:

```julia
x1 = Array{Union{Missing,Float64}}(undef,3)
for i = 1:3 
x1[i]=i
end

julia> x1
3-element Array{Union{Missing, Float64},1}:
 1.0
 2.0
 3.0

```

you can do this by allocating a new array, but there is another way?

---

<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:** [March 28, 2019, 7:31pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/2 "2019-03-28T19:31:38Z")

</div>

`identity.(x)` should be good. Or `map(identity, x)`.

Or

```julia
using Missings
disallowmissing(x)

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 28, 2019, 7:40pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/3 "2019-03-28T19:40:59Z")

</div>

AFAIK all solutions will allocate, unless you rely on internals (but don’t do that).

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [March 28, 2019, 7:55pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/4 "2019-03-28T19:55:20Z")

</div>

There’s an open issue for this but currently no “public” API for it:

> <https://github.com/JuliaLang/julia/issues/26681>
>
> As discussed in \[this thread\](https://discourse.julialang.org/t/missing-data-and…-namedtuple-compatibility/8136/91?) , it'd be useful to allow converting between \`Array{Union{T, Missing}, N}\` and \`Array{T, N}\` without copying the data whenever possible. From (my understanding of) the discourse discussion, it seems that conversion from\`Array{Union{T, Missing}, N}\` to \`Array{T, N}\` should always be possible without copying whereas the other direction depends on whether the already allocated region is big enough to fit the wider type.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 28, 2019, 9:40pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/5 "2019-03-28T21:40:05Z")

</div>

a related question, how do i detect that said vector has a missing value?, I’m using `in(true,(ismissing.(x)))==true` but i’m sure there is a simpler way

---

<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:** [March 28, 2019, 9:41pm UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/6 "2019-03-28T21:41:36Z")

</div>

`any(ismissing, x)` will do it.

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [March 29, 2019, 5:11am UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/7 "2019-03-29T05:11:31Z")

</div>

```julia
Missings.disallowmissing(x)
convert(Vector{Float64}, x)

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [March 29, 2019, 10:48am UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/8 "2019-03-29T10:48:42Z")

</div>

> [@longemen3000](#):
>
> there is a way to transform an array `x1` of Union{Missing,Float64} to an Array `x2` of Float64 without allocating a new array?

```julia
julia> a = Union{Float64,Missing}[1.1, 2.2]
2-element Array{Union{Missing, Float64},1}:
 1.1
 2.2

julia> using MappedArrays

julia> b = of_eltype(Float64, a)
2-element mappedarray(x->(MappedArrays.convert)($(Expr(:static_parameter, 1)), x), y->(MappedArrays.convert)($(Expr(:static_parameter, 1)), y), ::Array{Union{Missing, Float64},1}) with eltype Float64:
 1.1
 2.2

julia> eltype(b)
Float64

```

You can easily convince yourself that there is no copy of `a` made:

```julia
julia> b[1] = 200
200

julia> a
2-element Array{Union{Missing, Float64},1}:
 200.0
   2.2

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 29, 2019, 11:13am UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/9 "2019-03-29T11:13:17Z")

</div>

> [@tim.holy](#):
>
> You can easily convince yourself that there is no copy of `a` made:

Strictly speaking, there is no transformation either but a view, and the result is not an `Array{Float64}` (that was asked for) but something else.

That said, I think this may be the best solution under some circumstances, as it can be very efficient. So perhaps the original question should have been not how to get an `Array{Floa64}`, but an array with `Float64` `eltype`.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 3:29am UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/10 "2019-09-06T03:29:47Z")

</div>

> [@Nosferican](#):
>
> disallowmissing

This function is no longer working?

---

<div class="post-metadata">

**Author:** ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)\
**Post date:** [September 6, 2019, 3:35am UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/11 "2019-09-06T03:35:39Z")

</div>

It works just fine as far as I know. I just tested it and works. Did you have an issue?

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [September 6, 2019, 3:36am UTC](https://discourse.julialang.org/t/efficient-way-to-transform-array-union-missing-float64-to-array-float64/22475/12 "2019-09-06T03:36:18Z")

</div>

OH I misunderstood. I thought the function gets rid of `missing`s but turns out it just converts if there are no missings. Sorry my bad.
