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

Division DataArray{Int} / Int results in Array{Any}

Open anriseth opened this issue 7 years ago • 2 comments

I have come over a problem that depends on whether DataFrames decides that my column is of type Array{Union{Int64, Missings.Missing},1} or DataArrays.DataArray{Int64,1}. Dividing such arrays by an Int results in different element types in the resulting array. I can fix this by doing element-wise division, however, it seems to be unexpected behaviour to me?

In the below, y/minimum(y) results in an array with Anys, instead of Float64s as I expected.

julia> x
2-element Array{Union{Int64, Missings.Missing},1}:
 51
 41
julia> y
2-element DataArrays.DataArray{Int64,1}:
 51
 41
julia> x/minimum(x), y/minimum(y)
([1.2439, 1.0], Any[1.2439, 1.0])

julia> x./minimum(x), y./minimum(y)
([1.2439, 1.0], Union{Float64, Missings.Missing}[1.2439, 1.0])

anriseth avatar Jan 05 '18 16:01 anriseth

I can't reproduce here on git master and Julia 0.6.2:

julia> y=DataArray([1])
1-element DataArrays.DataArray{Int64,1}:
 1

julia> y/minimum(y)
1-element DataArrays.DataArray{Any,1}:
 1.0

julia> y./minimum(y)
1-element DataArrays.DataArray{Float64,1}:
 1.0

What versions are you using?

EDIT: Silly me, that's the same problem, I just read incorrectly.

nalimilan avatar Jan 05 '18 16:01 nalimilan

That's due to Base.promote_op(/, eltype(y), eltype(y)) returning Any. Looks like we should call Missings.T before calling promote_op, and do Union{T, Missing} again on the result.

nalimilan avatar Jan 05 '18 16:01 nalimilan