Skip to content

Commit

Permalink
Fix minimum and maximum in the presence of missing values (#35989)
Browse files Browse the repository at this point in the history
* Fix minimum and maximum in the presence of missing values

* Fix comment
  • Loading branch information
nalimilan authored Jun 8, 2020
1 parent 9d4565b commit d31962c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 5 additions & 5 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
12 changes: 12 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d31962c

Please sign in to comment.