Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert type Array{Union{Missing, Float64},1} to Array{Float64,1} in Julia

Tags:

julia

I have an array of floats having some missing values, hence its type is Array{Union{Missing, Float64},1}. Is there a command to convert the non-missing part into Array{Float64,1}?

like image 712
Sameeresque Avatar asked Sep 09 '26 01:09

Sameeresque


1 Answers

Here are three solutions, in order of preference (thanks to @BogumilKaminski for the first one):

f1(x) = collect(skipmissing(x))
f2(x) = Float64[ a for a in x if !ismissing(a) ]
f3(x) = x[.!ismissing.(x)]

f1 lazy-loads the array with skipmissing (useful for e.g. iteration) and then builds the array via collect.

f2 uses a for loop but is likely to be slower than f1 since the final array length is not computed ahead of time.

f3 uses broadcasting, and allocates temporaries in the process, and so is likely to be the slowest of the three.

We can verify the above with a simple benchmark:

using BenchmarkTools
x = Array{Union{Missing,Float64}}(undef, 100);
inds = unique(rand(1:100, 50));
x[inds] = randn(length(inds));
@btime f1($x);
@btime f2($x);
@btime f3($x);

Resulting in:

julia> @btime f1($x);
  377.186 ns (7 allocations: 1.22 KiB)

julia> @btime f2($x);
  471.204 ns (8 allocations: 1.23 KiB)

julia> @btime f3($x);
  732.726 ns (6 allocations: 4.80 KiB)
like image 69
Colin T Bowers Avatar answered Sep 12 '26 19:09

Colin T Bowers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!