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

Add convert(Array, ::CategoricalArray) methods

Open nalimilan opened this issue 6 years ago • 2 comments

We should add convert methods so that in the following example, the element type is String:

julia> Array(categorical(["a"]))
1-element Array{CategoricalArrays.CategoricalValue{String,UInt32},1}:
 "a"

It would be great if we could find a way to make it work for any AbstractArray.

nalimilan avatar Oct 13 '17 20:10 nalimilan

If I'm not mistaken MyArrayType(a::AbstractArray) takes priority over convert(::Type{MyArrayType}, a::CategoricalArray) since julia searches for a matching constructor before the fallback to convert.

Since Array has a method with this signature https://github.com/JuliaLang/julia/blob/56b9593d8c25b839511e33e1fb06b58f95ce15df/base/boot.jl#L388 I don't see how we could make eltype(Array(categorical(["a"]))) == String through a convert method but we would have to restort to Array(x::AbstractCategoricalArray).

Array(x::CategoricalArray{T}) where {T} = convert(Array{T}, x) does the trick for Array, but not for any AbstractArray.

gustafsson avatar Feb 02 '18 10:02 gustafsson

Fixed by #123. We could imagine providing a generic construction method for all AbstractArray types but I'm not sure it's possible.

nalimilan avatar Feb 02 '18 20:02 nalimilan