Skip to content

Commit

Permalink
Skip missing values in describe (JuliaLang#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan authored Mar 2, 2019
1 parent c4ea78c commit 0d1fe72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
30 changes: 14 additions & 16 deletions src/scalarstats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -562,34 +562,32 @@ median, 75th percentile, and maxmimum.
function summarystats(a::AbstractArray{T}) where T<:Union{Real,Missing}
# `mean` doesn't fail on empty input but rather returns `NaN`, so we can use the
# return type to populate the `SummaryStats` structure.
m = mean(a)
s = T >: Missing ? collect(skipmissing(a)) : a
m = mean(s)
R = typeof(m)
n = length(a)
qs = if n == 0
ns = length(s)
qs = if m == 0 || n == 0
R[NaN, NaN, NaN, NaN, NaN]
elseif ismissing(m)
[missing, missing, missing, missing, missing]
elseif T >: Missing
quantile!(s, [0.00, 0.25, 0.50, 0.75, 1.00])
else
quantile(a, [0.00, 0.25, 0.50, 0.75, 1.00])
quantile(s, [0.00, 0.25, 0.50, 0.75, 1.00])
end
SummaryStats{R}(m, qs..., n, count(ismissing, a))
SummaryStats{R}(m, qs..., n, n - ns)
end

function Base.show(io::IO, ss::SummaryStats)
println(io, "Summary Stats:")
@printf(io, "Length: %i\n", ss.nobs)
ss.nobs > 0 || return
@printf(io, "Missing Count: %i\n", ss.nmiss)
if ss.nmiss > 0
println(io, "(All summary stats are missing)")
else
@printf(io, "Mean: %.6f\n", ss.mean)
@printf(io, "Minimum: %.6f\n", ss.min)
@printf(io, "1st Quartile: %.6f\n", ss.q25)
@printf(io, "Median: %.6f\n", ss.median)
@printf(io, "3rd Quartile: %.6f\n", ss.q75)
@printf(io, "Maximum: %.6f\n", ss.max)
end
@printf(io, "Mean: %.6f\n", ss.mean)
@printf(io, "Minimum: %.6f\n", ss.min)
@printf(io, "1st Quartile: %.6f\n", ss.q25)
@printf(io, "Median: %.6f\n", ss.median)
@printf(io, "3rd Quartile: %.6f\n", ss.q75)
@printf(io, "Maximum: %.6f\n", ss.max)
end


Expand Down
7 changes: 6 additions & 1 deletion test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ describe(io, Union{Float32,Missing}[1.0, 4.5, missing, missing, 33.1])
Summary Stats:
Length: 5
Missing Count: 2
(All summary stats are missing)
Mean: 12.866666
Minimum: 1.000000
1st Quartile: 2.750000
Median: 4.500000
3rd Quartile: 18.799999
Maximum: 33.099998
Type: $(Union{Float32,Missing})
"""

Expand Down

0 comments on commit 0d1fe72

Please sign in to comment.