Skip to content

Commit

Permalink
Fix remaining Julia 0.7 deprecations
Browse files Browse the repository at this point in the history
Use undef constructors, something() instead of coalesce(), LinearIndices instead of linearindices,
fix showcompact deprecation and avoid converting Symbol to String.
Also fix an inference regression (JuliaLang/julia#26771).
  • Loading branch information
nalimilan committed Jun 13, 2018
1 parent 73b852d commit 5a75135
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 58 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.6
Missings
Reexport
Compat 0.61.0
Compat 0.67.0
JSON
8 changes: 4 additions & 4 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ function copyto!(dest::CatArrOrSub{T, N}, dstart::Integer,
n::Integer) where {T, N}
n == 0 && return dest
n < 0 && throw(ArgumentError(string("tried to copy n=", n, " elements, but n should be nonnegative")))
destinds, srcinds = linearindices(dest), linearindices(src)
destinds, srcinds = LinearIndices(dest), LinearIndices(src)
(dstart destinds && dstart+n-1 destinds) || throw(BoundsError(dest, dstart:dstart+n-1))
(sstart srcinds && sstart+n-1 srcinds) || throw(BoundsError(src, sstart:sstart+n-1))

Expand Down Expand Up @@ -526,8 +526,8 @@ function vcat(A::CategoricalArray...)
newlevels, ordered = mergelevels(ordered, map(levels, A)...)

refsvec = map(A) do a
ii = indexin(index(a.pool), newlevels)
[x==0 ? 0 : ii[x] for x in a.refs]
ii = convert(Vector{Int}, indexin(index(a.pool), newlevels))
[x==0 ? 0 : ii[x] for x in a.refs]::Array{Int,ndims(a)}
end

T = Base.promote_eltype(A...) >: Missing ?
Expand Down Expand Up @@ -609,7 +609,7 @@ function levels!(A::CategoricalArray{T}, newlevels::Vector; allow_missing=false)
levelsmap = similar(A.refs, length(oldindex)+1)
# 0 maps to a missing value
levelsmap[1] = 0
levelsmap[2:end] .= coalesce.(indexin(oldindex, index(A.pool)), 0)
levelsmap[2:end] .= something.(indexin(oldindex, index(A.pool)), 0)

@inbounds for (i, x) in enumerate(A.refs)
A.refs[i] = levelsmap[x+1]
Expand Down
9 changes: 7 additions & 2 deletions src/extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,13 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector;

n = length(breaks)
if isempty(labels)
from = map(x -> sprint(showcompact, x), breaks[1:n-1])
to = map(x -> sprint(showcompact, x), breaks[2:n])
@static if VERSION >= v"0.7.0-DEV.4524"
from = map(x -> sprint(show, x, context=:compact=>true), breaks[1:n-1])
to = map(x -> sprint(show, x, context=:compact=>true), breaks[2:n])
else
from = map(x -> sprint(showcompact, x), breaks[1:n-1])
to = map(x -> sprint(showcompact, x), breaks[2:n])
end
levs = Vector{String}(undef, n-1)
for i in 1:n-2
levs[i] = string("[", from[i], ", ", to[i], ")")
Expand Down
2 changes: 1 addition & 1 deletion src/recode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function recode(a::AbstractArray, default::Any, pairs::Pair...)
# assume the caller wants to recode only some values to missing,
# but accept original values
if T === Missing && !isa(default, Missing)
dest = Array{Union{eltype(a), Missing}}(size(a))
dest = Array{Union{eltype(a), Missing}}(undef, size(a))
# Exception 2: if original array accepted missing values and missing does not appear
# in one of the pairs' LHS, result must accept missing values
elseif T >: Missing || default isa Missing || (eltype(a) >: Missing && !keytype_hasmissing(pairs...))
Expand Down
6 changes: 3 additions & 3 deletions test/12_missingarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ end
end

@testset "vcat with all empty array" begin
ca1 = CategoricalArray(0)
ca1 = CategoricalArray(undef, 0)
ca2 = CategoricalArray([missing, "b"])
r = vcat(ca1, ca2)
@test r [missing, "b"]
Expand All @@ -1043,7 +1043,7 @@ end
end

@testset "vcat with all missings and empty" begin
ca1 = CategoricalArray(0)
ca1 = CategoricalArray(undef, 0)
ca2 = CategoricalArray([missing, missing])
r = vcat(ca1, ca2)
@test r [missing, missing]
Expand All @@ -1056,7 +1056,7 @@ end
@test isordered(r)

ca1 = CategoricalArray(["a", missing])
ca2 = CategoricalArray{Union{String, Missing}}(2)
ca2 = CategoricalArray{Union{String, Missing}}(undef, 2)
ordered!(ca1, true)
@test isempty(levels(ca2))
r = vcat(ca1, ca2)
Expand Down
38 changes: 19 additions & 19 deletions test/13_arraycommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ end
@testset "copy src not supporting missings into dest not supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -279,7 +279,7 @@ end
@testset "copy src supporting missings into dest not supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{Missing, String}}(v), reverse(v))
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -288,7 +288,7 @@ end
@testset "copy src not supporting missings into dest supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
dest = CategoricalVector{Union{String, Missing}}(3)
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -297,7 +297,7 @@ end
@testset "copy src supporting missings into dest supporting missings" begin
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{String, Missing}}(v), reverse(v))
dest = CategoricalVector{Union{String, Missing}}(3)
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -307,7 +307,7 @@ end
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -317,7 +317,7 @@ end
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{String, Missing}}(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -327,7 +327,7 @@ end
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{Union{String, Missing}}(3)
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -337,7 +337,7 @@ end
v = ["a", "b", "c"]
src = levels!(CategoricalVector{Union{String, Missing}}(v), reverse(v))
vsrc = view(src, 1:length(src))
dest = CategoricalVector{Union{String, Missing}}(3)
dest = CategoricalVector{Union{String, Missing}}(undef, 3)
copyf!(dest, vsrc)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -347,13 +347,13 @@ end
v = ["a", "b", "c"]
src = levels!(CategoricalVector(v), reverse(v))
vsrc = view(src, 1:2)
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, vsrc)
@test dest[1:2] == src[1:2]
@test levels(dest) == levels(src)

vsrc = view(src, 1:2)
dest = CategoricalVector{String}(2)
dest = CategoricalVector{String}(undef, 2)
copyf!(dest, vsrc)
@test dest == src[1:2]
@test levels(dest) == levels(src)
Expand Down Expand Up @@ -401,7 +401,7 @@ end
v = ["a", "b", "c"]
src = CategoricalVector{Union{eltype(v), Missing}}(v)
levels!(src, reverse(v))
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -413,14 +413,14 @@ end

src = CategoricalVector{AbstractString}(v)
levels!(src, reverse(v))
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)

src = CategoricalVector{String}(v)
levels!(src, reverse(v))
dest = CategoricalVector{AbstractString}(3)
dest = CategoricalVector{AbstractString}(undef, 3)
copyf!(dest, src)
@test dest == src
@test levels(dest) == levels(src) == reverse(v)
Expand All @@ -429,15 +429,15 @@ end
@testset "inviable mixed src and dest types" begin
v = ["a", "b", missing]
src = CategoricalVector(v)
dest = CategoricalVector{String}(3)
dest = CategoricalVector{String}(undef, 3)
@test_throws MissingException copyf!(dest, src)

vsrc = view(src, 1:length(src))
@test_throws MissingException copyf!(dest, vsrc)

v = Integer[-1, -2, -3]
src = CategoricalVector(v)
dest = CategoricalVector{UInt}(3)
dest = CategoricalVector{UInt}(undef, 3)
@test_throws InexactError copyf!(dest, src)
end
end
Expand Down Expand Up @@ -532,7 +532,7 @@ end
@test x2 == x
end

fill!(x2, :c)
fill!(x2, "c")
@test x2 == ["c", "c", "c"]
@test levels(x2) == ["a", "b", "c"]

Expand Down Expand Up @@ -727,7 +727,7 @@ end
end

@testset "converting from array with missings to array without missings CategoricalArray fails with missings" begin
x = CategoricalArray{Union{String, Missing}}(1)
x = CategoricalArray{Union{String, Missing}}(undef, 1)
@test_throws MissingException CategoricalArray{String}(x)
@test_throws MissingException convert(CategoricalArray{String}, x)
end
Expand Down Expand Up @@ -808,8 +808,8 @@ end

@testset "vcat() takes into account element type even when array is empty" begin
# or when both arrays have the same levels but of different types
x = CategoricalVector{String}(0)
y = CategoricalVector{Int}(0)
x = CategoricalVector{String}(undef, 0)
y = CategoricalVector{Int}(undef, 0)
z1 = CategoricalVector{Float64}([1.0])
z2 = CategoricalVector{Int}([1])
@inferred vcat(x, y)
Expand Down
Loading

0 comments on commit 5a75135

Please sign in to comment.