Skip to content

Commit 320411b

Browse files
authored
Fix #631, summarystats returned NaN quantiles for arrays with mean 0 (#632)
1 parent 41669cd commit 320411b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/scalarstats.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ function summarystats(a::AbstractArray{T}) where T<:Union{Real,Missing}
660660
R = typeof(m)
661661
n = length(a)
662662
ns = length(s)
663-
qs = if m == 0 || n == 0
663+
qs = if ns == 0
664664
R[NaN, NaN, NaN, NaN, NaN]
665665
elseif T >: Missing
666666
quantile!(s, [0.00, 0.25, 0.50, 0.75, 1.00])

test/scalarstats.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,31 @@ s = summarystats(1:5)
217217
@test s.median 3.0
218218
@test s.q25 2.0
219219
@test s.q75 4.0
220+
221+
# Issue #631
222+
s = summarystats([-2, -1, 0, 1, 2, missing])
223+
@test isa(s, StatsBase.SummaryStats)
224+
@test s.min == -2.0
225+
@test s.max == 2.0
226+
@test s.mean 0.0
227+
@test s.median 0.0
228+
@test s.q25 -1.0
229+
@test s.q75 +1.0
230+
231+
# Issue #631
232+
s = summarystats(zeros(10))
233+
@test isa(s, StatsBase.SummaryStats)
234+
@test s.min == 0.0
235+
@test s.max == 0.0
236+
@test s.mean 0.0
237+
@test s.median 0.0
238+
@test s.q25 0.0
239+
@test s.q75 0.0
240+
241+
# Issue #631
242+
s = summarystats(Union{Float64,Missing}[missing, missing])
243+
@test isa(s, StatsBase.SummaryStats)
244+
@test s.nobs == 2
245+
@test s.nmiss == 2
246+
@test isnan(s.mean)
247+
@test isnan(s.median)

0 commit comments

Comments
 (0)