Get rid of Missing in eltype

#1

I tried but couldn’t find an efficient way to “convert” a v::Vector{Union{Missing, T}} to Vector{T} if there are if fact no missings, and throw an error otherwise. [v...] works for converting, but as I understand it is inefficient, and also hangs up (takes a very very long time) if some elements are missing.

#2

convert(Vector{T}, v) should work:

julia> v = Union{Int,Missing}[1, 2, 3];

julia> convert(Vector{Int}, v)
3-element Array{Int64,1}:
 1
 2
 3

Splatting (...) is slow with many arguments. Another way would be to use collect(T, v):

julia> v = Union{Int,Missing}[1, 2, 3];

julia> collect(Int, v)
3-element Array{Int64,1}:
 1
 2
 3
#3

Thank you! Indeed, this implementation seems to work:

nomissing(vec::Vector{Union{Missing, T}}) where {T} = convert(Vector{T}, vec)
nomissing(vec::Vector) = vec

And as for splatting: is there another, more efficient, way to infer the actual eltype, but for splatting? E.g. convert Vector{Any} with only Float64 values inside to Vector{Float64}.

#4
julia> using Missings

julia> obj = Vector{Union{Int,Missing}}(1:3)
3-element Array{Union{Missing, Int64},1}:
 1
 2
 3

julia> disallowmissing(obj)
3-element Array{Int64,1}:
 1
 2
 3

The function is exported in packages like DataFrames so don’t be surprised if it accessible even if not calling Missings explicitly.

#5

Wow, never heard of that function! I thought that Missings is obsolete since missing value handling is in base julia, but it turns out to add some features.

1 Like
#6

Initially, all it’s functionality was moved to Base, but since then, there have been a few new things to “try out” in the Missings.jl package as a trial before going into Base. So most people/packages probably don’t need a dependency on Missings, but if you want to use some of these “trial” features, then it can make sense.

1 Like
#7

It’s kind of implied in the README (“additional features”), but it would be great to make it more explicit.