Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There should be a zero(::Missing) method #28854

Closed
yurivish opened this issue Aug 23, 2018 · 3 comments
Closed

There should be a zero(::Missing) method #28854

yurivish opened this issue Aug 23, 2018 · 3 comments
Labels
maths Mathematical functions missing data Base.missing and related functionality

Comments

@yurivish
Copy link
Contributor

yurivish commented Aug 23, 2018

zero(Missing) currently fails, as does one(Missing) and oneunit(Missing):

julia> zero(Missing)
ERROR: UndefVarError: T not defined

The error comes from this code in base/missing.jl:

for f in (:(Base.zero), :(Base.one), :(Base.oneunit))
    @eval function $(f)(::Type{Union{T, Missing}}) where T
        T === Any && throw(MethodError($f, (Any,)))  # To prevent StackOverflowError
        $f(T)
    end
end

Perhaps zero(::Missing) and one(::Missing) should be missing, since missing + missing == missing and the same for * and those two methods give you the additive and multiplicative identities.

@ararslan suggests adding @eval $(f)(::Type{Missing}) = missing to the loop.

@ararslan ararslan added maths Mathematical functions missing data Base.missing and related functionality labels Aug 23, 2018
@nalimilan
Copy link
Member

Good question. It sounds a bit weird that iszero(zero(missing)) and zero(missing) == 0 would not be true. But maybe that's not an issue. Can you show an actual use case?

@yha
Copy link
Contributor

yha commented Jun 5, 2019

I just encountered this when doing correlation plots (from StatsPlots).
It seems to arise easily by calling Statistics.cor on data with missing. Reproducing example:

julia> import Statistics: cor
julia> cor([zeros(1,1); missing])
ERROR: UndefVarError: T not defined

EDIT: for this use case, missing seems to be the right answer (at least for oneunit):

Base.oneunit(::Type{Missing}) = missing
x = rand(10)
cor([[x;missing] [0.0;x] [x;0.0] [x;0.0]])

yields a reasonable answer

4×4 Array{Union{Missing, Float64},2}:
 1.0         missing     missing     missing 
  missing   1.0        -0.0904971  -0.0904971
  missing  -0.0904971   1.0         1.0      
  missing  -0.0904971   1.0         1.0      

@nalimilan
Copy link
Member

This has actually been fixed in March by #31303. That will be included in Julia 1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions missing data Base.missing and related functionality
Projects
None yet
Development

No branches or pull requests

4 participants