Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing types within a type union

Tags:

julia

Suppose I have a DataFrame in Julia and typeof((df[:,:col])) returns Array{Union{Missing, Float64},1}. How do I check the types within Union{Missing, Float64} to, for example, see if Float64 is in that Union, or to make sure that there are no String values in that Union?

like image 522
bug_spray Avatar asked Sep 15 '19 17:09

bug_spray


1 Answers

You can use the subtype operator:

T1 = Union{Missing, Float64}
Float64 <: T1 # true
String <: T1  # false

This is because Float64 is a subtype of the union, whereas String is not (since it's not in the union).

If you're defining a method to dispatch on it, you could go a step further:

function doSomething(arr::Vector{Union{Missing, T}}) where T <: Float64
    # do something
end
like image 132
Anshul Singhvi Avatar answered Oct 06 '22 01:10

Anshul Singhvi