Skip to content

Commit

Permalink
Fix tests for makeunique keyword and deprecated column indexing (#1363)
Browse files Browse the repository at this point in the history
Fix tests to take into account makeunique keyword and remove deprecated column indexing values.
  • Loading branch information
bkamins authored and nalimilan committed Feb 28, 2018
1 parent e0ef5ed commit 102c458
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 135 deletions.
11 changes: 0 additions & 11 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1296,17 +1296,6 @@ function Base.getindex(x::AbstractIndex, idx::Real)
Int(idx)
end

function Base.getindex(x::AbstractIndex, idx::AbstractRange)
Base.depwarn("Indexing with range of values that are not Integer is deprecated", :getindex)
getindex(x, collect(idx))
end


function Base.getindex(x::AbstractIndex, idx::AbstractRange{Bool})
Base.depwarn("Indexing with range of Bool is deprecated", :getindex)
collect(Int, idx)
end

import Base: vcat
@deprecate vcat(x::Vector{<:AbstractDataFrame}) vcat(x...)

8 changes: 6 additions & 2 deletions src/other/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,16 @@ end
function Base.getindex(x::AbstractIndex, idx::AbstractVector{Union{Bool, Missing}})
if any(ismissing, idx)
# TODO: this line should be changed to throw an error after deprecation
# throw(ArgumentError("missing values are not allowed for column indexing"))
Base.depwarn("using missing in column indexing is deprecated", :getindex)
end
getindex(x, collect(Missings.replace(idx, false)))
end

function Base.getindex(x::AbstractIndex, idx::AbstractVector{<:Integer})
# TODO: this line should be changed to throw an error after deprecation
if any(v -> v isa Bool, idx)
# TODO: this line should be changed to throw an error after deprecation
# throw(ArgumentError("Bool values except for Vector{Bool} are not allowed for column indexing"))
Base.depwarn("Indexing with Bool values is deprecated except for Vector{Bool}")
end
Vector{Int}(idx)
Expand All @@ -151,15 +153,17 @@ end
# catch all method handling cases when type of idx is not narrowest possible, Any in particular
# also it handles passing missing values in idx
function Base.getindex(x::AbstractIndex, idx::AbstractVector)
# TODO: passing missing will throw an error after deprecation
idxs = filter(!ismissing, idx)
if length(idxs) != length(idx)
# TODO: passing missing will throw an error after deprecation
# throw(ArgumentError("missing values are not allowed for column indexing"))
Base.depwarn("using missing in column indexing is deprecated", :getindex)
end
length(idxs) == 0 && return Int[] # special case of empty idxs
if idxs[1] isa Real
if !all(v -> v isa Integer && !(v isa Bool), idxs)
# TODO: this line should be changed to throw an error after deprecation
# throw(ArgumentError("Only Integer values allowed when indexing by vector of numbers"))
Base.depwarn("indexing by vector of numbers other than Integer is deprecated", :getindex)
end
return Vector{Int}(idxs)
Expand Down
29 changes: 16 additions & 13 deletions test/cat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,55 @@ module TestCat
df4 = convert(DataFrame, [1:4 1:4])
df5 = DataFrame(Any[Union{Int, Missing}[1,2,3,4], nvstr])

dfh = hcat(df3, df4)
dfh = hcat(df3, df4, makeunique=true)
@test size(dfh, 2) == 3
@test names(dfh) [:x1, :x1_1, :x2]
@test dfh[:x1] df3[:x1]
@test dfh [df3 df4]
@test dfh DataFrames.hcat!(DataFrame(), df3, df4)
@test dfh DataFrames.hcat!(DataFrame(), df3, df4, makeunique=true)

dfh3 = hcat(df3, df4, df5)
dfa = DataFrame(a=[1,2])
dfb = DataFrame(b=[3,missing])
@test hcat(dfa, dfb) [dfa dfb]

dfh3 = hcat(df3, df4, df5, makeunique=true)
@test names(dfh3) == [:x1, :x1_1, :x2, :x1_2, :x2_1]
@test dfh3 hcat(dfh, df5)
@test dfh3 DataFrames.hcat!(DataFrame(), df3, df4, df5)
@test dfh3 hcat(dfh, df5, makeunique=true)
@test dfh3 DataFrames.hcat!(DataFrame(), df3, df4, df5, makeunique=true)

@test df2 DataFrames.hcat!(df2)

@testset "hcat ::AbstractDataFrame" begin
df = DataFrame(A = repeat('A':'C', inner=4), B = 1:12)
gd = groupby(df, :A)
answer = DataFrame(A = fill('A', 4), B = 1:4, A_1 = 'B', B_1 = 5:8, A_2 = 'C', B_2 = 9:12)
@test hcat(gd...) == answer
@test hcat(gd..., makeunique=true) == answer
answer = answer[1:4]
@test hcat(gd[1], gd[2]) == answer
@test hcat(gd[1], gd[2], makeunique=true) == answer
end

@testset "hcat ::AbstractDataFrame" begin
df = DataFrame(A = repeat('A':'C', inner=4), B = 1:12)
gd = groupby(df, :A)
answer = DataFrame(A = fill('A', 4), B = 1:4, A_1 = 'B', B_1 = 5:8, A_2 = 'C', B_2 = 9:12)
@test hcat(gd...) == answer
@test hcat(gd..., makeunique=true) == answer
answer = answer[1:4]
@test hcat(gd[1], gd[2]) == answer
@test hcat(gd[1], gd[2], makeunique=true) == answer
end

@testset "hcat ::AbstractVectors" begin
df = DataFrame()
DataFrames.hcat!(df, CategoricalVector{Union{Int, Missing}}(1:10))
@test df[1] == CategoricalVector(1:10)
DataFrames.hcat!(df, 1:10)
DataFrames.hcat!(df, 1:10, makeunique=true)
@test df[2] == collect(1:10)
DataFrames.hcat!(df, collect(1:10))
DataFrames.hcat!(df, collect(1:10), makeunique=true)
@test df[3] == collect(1:10)

df = DataFrame()
df2 = hcat(CategoricalVector{Union{Int, Missing}}(1:10), df)
@test df2[1] == collect(1:10)
@test names(df2) == [:x1]
df3 = hcat(11:20, df2)
df3 = hcat(11:20, df2, makeunique=true)
@test df3[1] == collect(11:20)
@test names(df3) == [:x1, :x1_1]

Expand Down
4 changes: 2 additions & 2 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ module TestData
)
df2 = spltdf(df2)

m1 = join(df1, df2, on = :a)
m2 = join(df1, df2, on = [:x1, :x2, :x3])
m1 = join(df1, df2, on = :a, makeunique=true)
m2 = join(df1, df2, on = [:x1, :x2, :x3], makeunique=true)
@test sort(m1[:a]) == sort(m2[:a])

# test nonunique() with extra argument
Expand Down
2 changes: 2 additions & 0 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ module TestDataFrame

z = vcat(v, x)

# TODO: uncomment the line below after deprecation
# @test_throws ArgumentError z[:, [1, 1, 2]]
z2 = z[:, [1, 1, 2]]
@test names(z2) == [:a, :a_1, :b]

Expand Down
42 changes: 32 additions & 10 deletions test/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@ push!(i, :A)
push!(i, :B)

inds = Any[1,
1.0,
big(1),
:A,
[true, false],
[1],
[1.0],
1.0:1.0,
[big(1)],
big(1):big(1),
[:A],
Union{Bool, Missing}[true, false],
Union{Int, Missing}[1],
Union{Float64, Missing}[1.0],
Union{BigInt, Missing}[big(1)],
Union{Symbol, Missing}[:A],
Any[1],
Any[1, missing],
Any[true, missing],
Any[:A],
Any[:A, missing],
[true, missing]]
Any[:A]]

for ind in inds
if ind == :A || ndims(ind) == 0
Expand All @@ -32,6 +28,32 @@ for ind in inds
end
end

# TODO: change this to throw an error after deprecation
# @test_throws MethodError i[1.0]
# @test_throws MethodError i[true]
# @test_throws MethodError i[false]
# @test_throws ArgumentError i[Any[1, missing]]
# @test_throws ArgumentError i[[1, missing]]
# @test_throws ArgumentError i[[true, missing]]
# @test_throws ArgumentError i[Any[true, missing]]
# @test_throws ArgumentError i[[:A, missing]]
# @test_throws ArgumentError i[Any[:A, missing]]
# @test_throws ArgumentError i[1.0:1.0]
# @test_throws ArgumentError i[[1.0]]
# @test_throws ArgumentError i[Any[1.0]]
inds = Any[1.0, true, false,
Any[1, missing], [1, missing],
[true, missing], Any[true, missing],
[:A, missing], Any[:A, missing],
1.0:1.0, [1.0], Any[1.0]]
for ind in inds
if ind == :A || ndims(ind) == 0
@test i[ind] == 1
else
@test (i[ind] == [1])
end
end

@test i[1:1] == 1:1

@test_throws BoundsError i[[true]]
Expand All @@ -45,7 +67,7 @@ end
@test i[Symbol[]] == Int[]

@test names(i) == [:A,:B]
@test names!(i, [:a,:a], allow_duplicates=true) == Index([:a,:a_1])
@test names!(i, [:a,:a], makeunique=true) == Index([:a,:a_1])
@test_throws ArgumentError names!(i, [:a,:a])
@test names!(i, [:a,:b]) == Index([:a,:b])
@test rename(i, Dict(:a=>:A, :b=>:B)) == Index([:A,:B])
Expand Down
Loading

0 comments on commit 102c458

Please sign in to comment.