Skip to content

quantile is broken with unitful quantities #883

@juliohm

Description

@juliohm

The quantile function with unitful quantities works fine without weights:

julia> x = rand(10) .* 1u"m"
10-element Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}:
   0.4560455822873759 m
   0.4307569757744304 m
   0.4840857672199611 m
   0.7853776015820557 m
   0.5865440640498318 m
  0.15996668178343565 m
 0.042204899384880146 m
   0.3310111766347721 m
   0.8691765138294111 m
   0.5325561348479884 m

julia> quantile(x, 0.5)
0.4700656747536685 m

However, if we try to pass weights, it crashes:

julia> quantile(x, Weights(1:10), 0.5)
ERROR: MethodError: no method matching quantile(::Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, ::Weights{Int64, Int64, UnitRange{Int64}}, ::Float64)

Closest candidates are:
  quantile(::AbstractVector{T} where T<:Real, ::AbstractWeights{var"#s42", T} where {var"#s42"<:Real, T<:Real}, ::Number)
   @ StatsBase ~/.julia/packages/StatsBase/XgjIN/src/weights.jl:772
  quantile(::AbstractVector{V}, ::AbstractWeights{W, T} where T<:Real, ::AbstractVector{T} where T<:Real) where {V, W<:Real}
   @ StatsBase ~/.julia/packages/StatsBase/XgjIN/src/weights.jl:700
  quantile(::AbstractVector, ::Any; sorted, alpha, beta)
   @ Statistics ~/.julia/juliaup/julia-1.9.2+0.x64.linux.gnu/share/julia/stdlib/v1.9/Statistics/src/Statistics.jl:1073
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

This method definition is too restrictive:

function quantile(v::AbstractVector{<:Real}{V}, w::AbstractWeights{W}, p::AbstractVector{<:Real}) where {V,W<:Real}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions