From d31962cf63b9bf5b42e6b28137adf0b37bb21c5e Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Mon, 8 Jun 2020 05:59:53 +0200 Subject: [PATCH] Fix minimum and maximum in the presence of missing values (#35989) * Fix minimum and maximum in the presence of missing values * Fix comment --- base/reduce.jl | 10 +++++----- test/reduce.jl | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index 6d19fa50e3037..6582e72274aba 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -576,11 +576,11 @@ function mapreduce_impl(f, op::Union{typeof(max), typeof(min)}, start = first + 1 simdstop = start + chunk_len - 4 while simdstop <= last - 3 - # short circuit in case of NaN - v1 == v1 || return v1 - v2 == v2 || return v2 - v3 == v3 || return v3 - v4 == v4 || return v4 + # short circuit in case of NaN or missing + (v1 == v1) === true || return v1 + (v2 == v2) === true || return v2 + (v3 == v3) === true || return v3 + (v4 == v4) === true || return v4 @inbounds for i in start:4:simdstop v1 = _fast(op, v1, f(A[i+0])) v2 = _fast(op, v2, f(A[i+1])) diff --git a/test/reduce.jl b/test/reduce.jl index 9501f41f8b77c..d640bcdde7440 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -341,6 +341,18 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1)) @test size(extrema(A,dims=(1,2,3))) == size(maximum(A,dims=(1,2,3))) @test extrema(x->div(x, 2), A, dims=(2,3)) == reshape([(0,11),(1,12)],2,1,1) +@testset "maximum/minimum/extrema with missing values" begin + for x in (Vector{Union{Int,Missing}}(missing, 10), + Vector{Union{Int,Missing}}(missing, 257)) + @test maximum(x) === minimum(x) === missing + @test extrema(x) === (missing, missing) + fill!(x, 1) + x[1] = missing + @test maximum(x) === minimum(x) === missing + @test extrema(x) === (missing, missing) + end +end + # any & all @test @inferred any([]) == false