Skip to content

Commit ca8369d

Browse files
committed
Fix NaN and missing detection in quantile()
When `sort=false`, we only partially sort the input, so `NaN`/`missing` is not guaranteed to be in the last position. Also avoid throwing errors for non-`Number` types, for which `isnan` may not be defined.
1 parent 05f09fe commit ca8369d

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Statistics.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,10 @@ function _quantilesort!(v::AbstractArray, sorted::Bool, minp::Real, maxp::Real)
960960
# only need to perform partial sort
961961
sort!(v, 1, lv, Base.Sort.PartialQuickSort(lo:hi), Base.Sort.Forward)
962962
end
963-
ismissing(v[end]) && throw(ArgumentError("quantiles are undefined in presence of missing values"))
964-
isnan(v[end]) && throw(ArgumentError("quantiles are undefined in presence of NaNs"))
963+
if (sorted && (ismissing(v[end]) || (v[end] isa Number && isnan(v[end])))) ||
964+
any(x -> ismissing(x) || (x isa Number && isnan(x)), v)
965+
throw(ArgumentError("quantiles are undefined in presence of NaNs or missing values"))
966+
end
965967
return v
966968
end
967969

test/runtests.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,15 @@ end
557557
@test quantile(Any[1, Float16(2), 3], Float16(0.5)) isa Float16
558558
@test quantile(Any[1, big(2), 3], Float16(0.5)) isa BigFloat
559559

560-
@test_throws ArgumentError quantile([1, missing], 0.5)
561-
@test_throws ArgumentError quantile([1, NaN], 0.5)
560+
# Need a large vector to actually check consequences of partial sorting
561+
x = rand(50)
562+
for sorted in (false, true)
563+
x[10] = NaN
564+
@test_throws ArgumentError quantile(x, 0.5, sorted=sorted)
565+
x = Vector{Union{Float64, Missing}}(x)
566+
x[10] = missing
567+
@test_throws ArgumentError quantile(x, 0.5, sorted=sorted)
568+
end
562569
@test quantile(skipmissing([1, missing, 2]), 0.5) === 1.5
563570
@test quantile([1], 0.5) === 1.0
564571

0 commit comments

Comments
 (0)