Skip to content

Commit e5bc8f0

Browse files
authored
Merge pull request JuliaData#104 from alyst/add_fill
add fill!(CatArray, val)
2 parents 8d086f6 + 53acb30 commit e5bc8f0

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/array.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ Base.IndexStyle(::Type{<:CategoricalArray}) = IndexLinear()
338338
@inbounds A.refs[I...] = get!(A.pool, v)
339339
end
340340

341+
Base.fill!(A::CategoricalArray, v::Any) =
342+
(fill!(A.refs, get!(A.pool, convert(leveltype(A), v))); A)
343+
341344
function mergelevels(ordered, levels...)
342345
T = Base.promote_eltype(levels...)
343346
res = Array{T}(0)

src/missingarray.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ end
2222
@inbounds A.refs[I...] = 0
2323
end
2424

25+
Base.fill!(A::CategoricalArray{>:Missing}, ::Missing) = (fill!(A.refs, 0); A)
26+
2527
in(x::Missing, y::CategoricalArray) = false
2628
in(x::Missing, y::CategoricalArray{>:Missing}) = !all(v -> v > 0, y.refs)

test/13_arraycommon.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,37 @@ end
427427
@test !isordered(x)
428428
end
429429

430+
@testset "fill!()" begin
431+
x = CategoricalArray{Union{String, T}}(["a", "b", "c"])
432+
x2 = copy(x)
433+
@test fill!(x2, "a") === x2
434+
@test x2 == ["a", "a", "a"]
435+
@test levels(x2) == ["a", "b", "c"]
436+
437+
@test fill!(x2, x[2]) == ["b", "b", "b"]
438+
@test levels(x2) == ["a", "b", "c"]
439+
440+
x2 = copy(x)
441+
@test_throws MethodError fill!(x2, 3)
442+
@test x2 == x
443+
444+
if T === Missing
445+
x2 = fill!(copy(x), missing)
446+
@test all(ismissing, x2)
447+
else
448+
@test_throws MethodError fill!(x2, missing)
449+
@test x2 == x
450+
end
451+
452+
fill!(x2, :c)
453+
@test x2 == ["c", "c", "c"]
454+
@test levels(x2) == ["a", "b", "c"]
455+
456+
fill!(x2, "0")
457+
@test x2 == ["0", "0", "0"]
458+
@test levels(x2) == ["a", "b", "c", "0"]
459+
end
460+
430461
@testset "overflow of reftype is detected and doesn't corrupt data and levels" begin
431462
res = @test_throws LevelsException{Int, UInt8} CategoricalArray{Union{T, Int}, 1, UInt8}(256:-1:1)
432463
@test res.value.levels == [1]

0 commit comments

Comments
 (0)