CategoricalArrays.jl
CategoricalArrays.jl copied to clipboard
Add convert(Array, ::CategoricalArray) methods
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
.
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
.
Fixed by #123. We could imagine providing a generic construction method for all AbstractArray
types but I'm not sure it's possible.