Skip to content

Commit d31962c

Browse files
authored
Fix minimum and maximum in the presence of missing values (#35989)
* Fix minimum and maximum in the presence of missing values * Fix comment
1 parent 9d4565b commit d31962c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

base/reduce.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,11 +576,11 @@ function mapreduce_impl(f, op::Union{typeof(max), typeof(min)},
576576
start = first + 1
577577
simdstop = start + chunk_len - 4
578578
while simdstop <= last - 3
579-
# short circuit in case of NaN
580-
v1 == v1 || return v1
581-
v2 == v2 || return v2
582-
v3 == v3 || return v3
583-
v4 == v4 || return v4
579+
# short circuit in case of NaN or missing
580+
(v1 == v1) === true || return v1
581+
(v2 == v2) === true || return v2
582+
(v3 == v3) === true || return v3
583+
(v4 == v4) === true || return v4
584584
@inbounds for i in start:4:simdstop
585585
v1 = _fast(op, v1, f(A[i+0]))
586586
v2 = _fast(op, v2, f(A[i+1]))

test/reduce.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1))
341341
@test size(extrema(A,dims=(1,2,3))) == size(maximum(A,dims=(1,2,3)))
342342
@test extrema(x->div(x, 2), A, dims=(2,3)) == reshape([(0,11),(1,12)],2,1,1)
343343

344+
@testset "maximum/minimum/extrema with missing values" begin
345+
for x in (Vector{Union{Int,Missing}}(missing, 10),
346+
Vector{Union{Int,Missing}}(missing, 257))
347+
@test maximum(x) === minimum(x) === missing
348+
@test extrema(x) === (missing, missing)
349+
fill!(x, 1)
350+
x[1] = missing
351+
@test maximum(x) === minimum(x) === missing
352+
@test extrema(x) === (missing, missing)
353+
end
354+
end
355+
344356
# any & all
345357

346358
@test @inferred any([]) == false

0 commit comments

Comments
 (0)