Skip to content

Commit 4014e4a

Browse files
gustafssonnalimilan
authored andcommitted
Make collect() and Array() produce Array without categorical value eltype (JuliaData#123)
1 parent 248cd6c commit 4014e4a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/array.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Code for CategoricalArray
22

3-
import Base: convert, copy, copy!, getindex, setindex!, similar, size,
3+
import Base: Array, convert, collect, copy, copy!, getindex, setindex!, similar, size,
44
unique, vcat, in, summary
55

66
# Used for keyword argument default value
@@ -729,6 +729,9 @@ function in(x::CategoricalValue, y::CategoricalArray{T, N, R}) where {T, N, R}
729729
end
730730
end
731731

732+
Array(A::CategoricalArray{T}) where {T} = Array{T}(A)
733+
collect(A::CategoricalArray{T}) where {T} = Array{T}(A)
734+
732735
# Override AbstractArray method to avoid printing useless type parameters
733736
summary(A::CategoricalArray{T, N, R}) where {T, N, R} =
734737
string(Base.dims2string(size(A)), " $CategoricalArray{$T,$N,$R}")

test/13_arraycommon.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,4 +793,32 @@ end
793793
end
794794
end
795795

796+
@testset "collect of CategoricalArray produces Array" begin
797+
x = [1,1,2,2]
798+
y = categorical(x)
799+
z = collect(y)
800+
@test typeof(x) == typeof(z)
801+
@test z == x
802+
803+
x = [1,1,2,missing]
804+
y = categorical(x)
805+
z = collect(y)
806+
@test typeof(x) == typeof(z)
807+
@test z x
808+
end
809+
810+
@testset "Array(::CategoricalArray{T}) produces Array{T}" begin
811+
x = [1,1,2,2]
812+
y = categorical(x)
813+
z = Array(y)
814+
@test typeof(x) == typeof(z)
815+
@test z == x
816+
817+
x = [1,1,2,missing]
818+
y = categorical(x)
819+
z = Array(y)
820+
@test typeof(x) == typeof(z)
821+
@test z x
822+
end
823+
796824
end

0 commit comments

Comments
 (0)