Statistics.jl icon indicating copy to clipboard operation
Statistics.jl copied to clipboard

Cannot compute quantile(x::Array{Millisecond,1})

Open ianfiske opened this issue 4 years ago • 3 comments

ms = Millisecond.(1:10)
quantile(ms, 0.5)

gives

ERROR: MethodError: no method matching isnan(::Millisecond)
Closest candidates are:
  isnan(::BigFloat) at mpfr.jl:881
  isnan(::Missing) at missing.jl:100
  isnan(::Float16) at float.jl:537
  ...
Stacktrace:
 [1] _quantilesort!(::Array{Millisecond,1}, ::Bool, ::Float64, ::Float64) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:960
 [2] quantile!(::Array{Millisecond,1}, ::Float64; sorted::Bool, alpha::Float64, beta::Float64) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:943
 [3] quantile(::Array{Millisecond,1}, ::Float64; sorted::Bool, alpha::Float64, beta::Float64) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:1052
 [4] quantile(::Array{Millisecond,1}, ::Float64) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Statistics/src/Statistics.jl:1052
 [5] top-level scope at REPL[14]:1
 [6] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1088

on Julia 1.5.2.

The root issue is of course

julia> isnan(Millisecond(1))
ERROR: MethodError: no method matching isnan(::Millisecond)
Closest candidates are:
  isnan(::BigFloat) at mpfr.jl:881
  isnan(::Missing) at missing.jl:100
  isnan(::Float16) at float.jl:537

since _quantilesort! calls isnan(x[end])

ianfiske avatar Sep 25 '20 13:09 ianfiske

Would

isnan(::TimePeriod) = false

be an acceptable fix?

ianfiske avatar Sep 25 '20 13:09 ianfiske

Unitful defines isnan https://github.com/PainterQubits/Unitful.jl/blob/2be7e0692cafd948b66132920ee407b73220717f/src/quantities.jl and base should perhaps define it for TimePeriods as well.

mschauer avatar Sep 25 '20 13:09 mschauer

As noted at https://github.com/JuliaLang/julia/pull/37748, I think we'd better check applicable or method_exists, as we don't want to define isnan on all types that quantile should support.

nalimilan avatar Sep 29 '20 12:09 nalimilan