Skip to content

Commit

Permalink
Statistics.median: return first NaN if there are any
Browse files Browse the repository at this point in the history
Fix #29900
  • Loading branch information
StefanKarpinski committed Nov 2, 2018
1 parent 13694f4 commit 989d0b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion stdlib/Statistics/src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,10 @@ Like [`median`](@ref), but may overwrite the input vector.
function median!(v::AbstractVector)
isempty(v) && throw(ArgumentError("median of an empty array is undefined, $(repr(v))"))
eltype(v)>:Missing && any(ismissing, v) && return missing
(eltype(v)<:AbstractFloat || eltype(v)>:AbstractFloat) && any(isnan, v) && return NaN
if eltype(v)<:AbstractFloat || eltype(v)>:AbstractFloat
i = findfirst(isnan, v)
i !== nothing && return v[i]
end
inds = axes(v, 1)
n = length(inds)
mid = div(first(inds)+last(inds),2)
Expand Down
7 changes: 7 additions & 0 deletions stdlib/Statistics/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ end
@test median!([1 2; 3 4]) == 2.5

@test invoke(median, Tuple{AbstractVector}, 1:10) == median(1:10) == 5.5

@test median(Float32[1.0, NaN, 3.0]) === NaN32
@test median(Float32[1.0, -NaN, 3.0]) === -NaN32
@test median(Float16[1.0, NaN, 3.0]) === NaN16
@test median(Float16[1.0, -NaN, 3.0]) === -NaN16
@test median(BigFloat[1.0, NaN, 3.0]) isa BigFloat
@test isequal(median(BigFloat[1.0, NaN, 3.0]), NaN)
end

@testset "mean" begin
Expand Down

0 comments on commit 989d0b4

Please sign in to comment.