Skip to content

Add Array constructors and convert methods #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/array.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Code for CategoricalArray

import Base: Array, convert, collect, copy, getindex, setindex!, similar, size,
import Base: Array, Vector, Matrix, convert, collect, copy, getindex,
setindex!, similar, size,
unique, unique!, vcat, in, summary, float, complex, copyto!

# Used for keyword argument default value
Expand Down Expand Up @@ -410,6 +411,12 @@ convert(::Type{CategoricalArray{T, N}}, A::CategoricalArray{T, N}) where {T, N}
convert(::Type{CategoricalArray{T}}, A::CategoricalArray{T}) where {T} = A
convert(::Type{CategoricalArray}, A::CategoricalArray) = A

convert(::Type{Array{S, N}}, A::CatArrOrSub{T, N}) where {S, T, N} =
collect(S, A)
convert(::Type{Array}, A::CatArrOrSub) = unwrap.(A)
convert(::Type{Vector}, A::CatArrOrSub) = unwrap.(A)
convert(::Type{Matrix}, A::CatArrOrSub) = unwrap.(A)

function Base.:(==)(A::CategoricalArray{S}, B::CategoricalArray{T}) where {S, T}
if size(A) != size(B)
return false
Expand Down Expand Up @@ -1054,8 +1061,10 @@ function in(x::CategoricalValue, y::CategoricalArray{T, N, R}) where {T, N, R}
end
end

Array(A::CategoricalArray{T}) where {T} = Array{T}(A)
collect(A::CategoricalArray) = copy(A)
Array(A::CatArrOrSub{T}) where {T} = Array{T}(A)
Vector(A::CatArrOrSub{T}) where {T} = Vector{T}(A)
Matrix(A::CatArrOrSub{T}) where {T} = Matrix{T}(A)
collect(A::CatArrOrSub) = copy(A)

# Defined for performance
collect(x::Base.SkipMissing{<: CatArrOrSub{T}}) where {T} =
Expand Down Expand Up @@ -1125,7 +1134,7 @@ function Base.sort!(v::CategoricalVector;
levs = eltype(v) >: Missing ?
eltype(v)[i == 0 ? missing : CategoricalValue(v.pool, i) for i in 0:length(v.pool)] :
eltype(v)[CategoricalValue(v.pool, i) for i in 1:length(v.pool)]
sortedlevs = sort!(Vector(view(levs, seen)), order=ord)
sortedlevs = sort!(Vector{eltype(levs)}(view(levs, seen)), order=ord)
levelsmap = something.(indexin(sortedlevs, levs))
j = 0
refs = v.refs
Expand Down
100 changes: 99 additions & 1 deletion test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1330,18 +1330,116 @@ end
@test levels(x) == [2, 1, 3, 4]
end

@testset "Array(::CategoricalArray{T}) produces Array{T}" begin
@testset "Array(::CatArrOrSub{T}) produces Array{T}" begin
x = [1,1,2,2]
y = categorical(x)
z = Array(y)
@test typeof(x) == typeof(z)
@test z == x
z = Array(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z == x

x = [1,1,2,missing]
y = categorical(x)
z = Array(y)
@test typeof(x) == typeof(z)
@test z ≅ x
z = Array(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z ≅ x

x = [1,1,2,2]
y = categorical(x)
z = Vector(y)
@test typeof(x) == typeof(z)
@test z == x
z = Vector(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z == x

x = [1,1,2,missing]
y = categorical(x)
z = Vector(y)
@test typeof(x) == typeof(z)
@test z ≅ x
z = Vector(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z ≅ x

x = [1 1 2 2]
y = categorical(x)
z = Matrix(y)
@test typeof(x) == typeof(z)
@test z == x
z = Matrix(view(x, :, 1:4))
@test typeof(x) == typeof(z)
@test z == x

x = [1 1 2 missing]
y = categorical(x)
z = Matrix(y)
@test typeof(x) == typeof(z)
@test z ≅ x
z = Matrix(view(x, :, 1:4))
@test typeof(x) == typeof(z)
@test z ≅ x
end

@testset "convert(Array, ::CatArrOrSub{T}) produces Array{T}" begin
x = [1,1,2,2]
y = categorical(x)
z = convert(Array, y)
@test typeof(x) == typeof(z)
@test z == x
z = Array(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z == x

x = [1,1,2,missing]
y = categorical(x)
z = convert(Array, y)
@test typeof(x) == typeof(z)
@test z ≅ x
z = Array(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z ≅ x

x = [1,1,2,2]
y = categorical(x)
z = convert(Vector, y)
@test typeof(x) == typeof(z)
@test z == x
z = Vector(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z == x

x = [1,1,2,missing]
y = categorical(x)
z = convert(Vector, y)
@test typeof(x) == typeof(z)
@test z ≅ x
z = Vector(view(x, 1:4))
@test typeof(x) == typeof(z)
@test z ≅ x

x = [1 1 2 2]
y = categorical(x)
z = convert(Matrix, y)
@test typeof(x) == typeof(z)
@test z == x
z = Matrix(view(x, :, 1:4))
@test typeof(x) == typeof(z)
@test z == x

x = [1 1 2 missing]
y = categorical(x)
z = convert(Matrix, y)
@test typeof(x) == typeof(z)
@test z ≅ x
z = Matrix(view(x, :, 1:4))
@test typeof(x) == typeof(z)
@test z ≅ x
end

@testset "Array{T} constructors and convert" begin
Expand Down
Loading