Skip to content

Commit

Permalink
Add fill! method for CategoricalArray views to fix .=
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Jun 10, 2018
1 parent c2744d1 commit e559445
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ else
end

pool(A::SubArray{<:Any, <:Any, <:CategoricalArray}) = A.parent.pool

function Base.fill!(A::SubArray{<:Any, <:Any, <:CategoricalArray}, v::Any)
r = get!(A.parent.pool, convert(leveltype(A.parent), v))
fill!(refs(A), r)
A
end

Base.fill!(A::SubArray{<:Any, <:Any, <:CategoricalArray{>:Missing}}, ::Missing) =
(fill!(refs(A), 0); A)
15 changes: 15 additions & 0 deletions test/14_view.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ using Compat
using Compat.Test
using CategoricalArrays

const = isequal

@testset "view($(CategoricalArray{Union{T, eltype(a)}}), $inds), ordered=$order construction" for
T in (Union{}, Missing), order in (true, false),
a in (1:10, 10:-1:1, ["a", "c", "b", "b", "a"]),
Expand Down Expand Up @@ -30,4 +32,17 @@ end
@test view(ca1, 1:2) != view(ca5, 1:2, 1:1)
end

@testset "fill! on view" for
a in (categorical(["c", "a", "b"]), categorical(["c", "a", missing]))
v = view(a, 1:2)
@test fill!(v, a[1]) == ["c", "c"]
@test fill!(v, "a") == ["a", "a"]
@test fill!(v, "d") == ["d", "d"]
if eltype(a) >: Missing
@test fill!(v, missing) [missing, missing]
else
@test_throws MethodError fill!(v, missing)
end
end

end

0 comments on commit e559445

Please sign in to comment.