Skip to content
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

Make collect(::CategoricalArray) return a CategoricalArray #252

Merged
merged 1 commit into from
Apr 8, 2020
Merged
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
5 changes: 3 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ similar(A::CategoricalArray{S, M, Q}, ::Type{Union{CategoricalValue{T}, Missing}
dims::NTuple{N, Int}) where {T, N, S, M, Q} =
CategoricalArray{Union{T, Missing}, N, Q}(undef, dims)

for A in (:Array, :Vector, :Matrix) # to fix ambiguities
# AbstractRange methods are needed since collect uses 1:1 as dummy array
for A in (:Array, :Vector, :Matrix, :AbstractRange)
@eval begin
similar(A::$A, ::Type{CategoricalValue{T, R}},
dims::NTuple{N, Int}=size(A)) where {T, R, N} =
Expand Down Expand Up @@ -738,7 +739,7 @@ function in(x::CategoricalValue, y::CategoricalArray{T, N, R}) where {T, N, R}
end

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

function float(A::CategoricalArray{T}) where T
if !isconcretetype(T)
Expand Down
24 changes: 17 additions & 7 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ end
y = similar(Vector{T}, (3,))
@test isa(y, Vector{T})
@test size(y) == (3,)

y = similar(1:1, Union{CategoricalValue{String, UInt8}, T})
@test isa(y, CategoricalVector{Union{String, T}, UInt8})
@test size(y) == (1,)

y = similar(1:1, Union{CategoricalValue{String, UInt8}, T}, (3,))
@test isa(y, CategoricalVector{Union{String, T}, UInt8})
@test size(y) == (3,)
end

@testset "copy" begin
Expand Down Expand Up @@ -1023,18 +1031,20 @@ end
end
end

@testset "collect of CategoricalArray produces Array" begin
@testset "collect of CategoricalArray produces CategoricalArray" begin
x = [1,1,2,2]
y = categorical(x)
z = collect(y)
@test typeof(x) == typeof(z)
@test z == x
for z in (collect(y), collect(eltype(y), y), collect(Iterators.take(y, 4)))
@test typeof(y) == typeof(z)
@test z == y == x
end

x = [1,1,2,missing]
y = categorical(x)
z = collect(y)
@test typeof(x) == typeof(z)
@test z ≅ x
for z in (collect(y), collect(eltype(y), y), collect(Iterators.take(y, 4)))
@test typeof(y) == typeof(z)
@test z ≅ y ≅ x
end
end

@testset "Array(::CategoricalArray{T}) produces Array{T}" begin
Expand Down