When should we use passmissing?

Dear all,

I find that whether or not use passmissing, the results are the same. So when should we use passmissing?Here are some examples.

julia> sqrt(missing)
missing

julia> passmissing(sqrt)(missing)
missing

julia> abs.([1, 2, missing])
3-element Vector{Union{Missing, Int64}}:
 1
 2
  missing

julia> passmissing(abs).([1, 2, missing])
3-element Vector{Union{Missing, Int64}}:
 1
 2
  missing

julia> (x->x^2).([1, 2, missing])
3-element Vector{Union{Missing, Int64}}:
 1
 4
  missing

julia> passmissing(x->x^2).([1, 2, missing])
3-element Vector{Union{Missing, Int64}}:
 1
 4
  missing

Please check this example.

1 Like

In the main its for “lifting” functions which don’t have a method to deal with Missing:

julia> parse.(Int, ["1", missing, "2"])
ERROR: MethodError: no method matching parse(::Type{Int64}, ::Missing)

versus

julia> passmissing(parse).(Int, ["1", missing, "2"])
3-element Vector{Union{Missing, Int64}}:
 1
  missing
 2

It essentially does f_passmissing(x) = ismissing(x) ? missing : f(x)

Missing handling is quite inconsistent in Base Julia, even across simple mathematical functions:

julia> sin(missing)
missing

julia> sind(missing)
ERROR: MethodError

That’s where passmissing or manual checks come in handy.

@rafael.guerra @aplavin @nilshg thanks for all of your help.