Sum of Any - Why sum of empty set is not zero?

Why sum of empty set is not zero ?

julia> eltype(dane)
Any
julia> sum(dane[:,9])
6.54132454e8
julia> dane[(mies20) .& (dane[:,7].==dealerzy[1]),9]
1-element Array{Any,1}:
 2032.0
julia> dane[(mies20) .& (dane[:,7].==dealerzy[2]),9]
Any[]

julia> sum(dane[(mies20) .& (dane[:,7].==dealerzy[2]),9])
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T at missing.jl:104
  zero(::Type{Measures.Length{:mm,Float64}}) at C:\Users\PC\.julia\packa
  zero(::Type{Measures.Length{:pct,Float64}}) at C:\Users\PC\.julia\pack
  ...

julia> isreal(dane[(mies20) .& (dane[:,7].==dealerzy[2]),9])
true

Paul

You can have sums of lots of objects, not just numbers. There is no reason to assume that if a vector is of type Any the default “zero” value should be 0.

4 Likes

Is it possible to set a Float type for one column of the matrix when the others are Any or String ?
Paul

You can do reduce(+, a, init=0.0) instead of sum(a) to force it to accumulate as floating point. (In Julia 1.6 you will be able to do sum(a, init=0.0) also.)

julia> reduce(+, Any[], init=0.0)
0.0
4 Likes

That sounds like you want a DataFrame from the DataFrames.jl package.

5 Likes

Or possibly a vector of structs

2 Likes