From 598f3815f379a039591c1f4698abe0c42df9fc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Ekl=C3=B6f?= <184952+gustafsson@users.noreply.github.com> Date: Tue, 25 Oct 2022 11:09:52 +0200 Subject: [PATCH] DimensionMismatch instead of ArgumentError on new columns with a different length --- src/dataframe/dataframe.jl | 2 +- src/subdataframe/subdataframe.jl | 3 +-- test/broadcasting.jl | 6 +++--- test/indexing.jl | 10 ++++----- test/select.jl | 34 +++++++++++++++--------------- test/subdataframe_mutation.jl | 36 ++++++++++++++++---------------- 6 files changed, 45 insertions(+), 46 deletions(-) diff --git a/src/dataframe/dataframe.jl b/src/dataframe/dataframe.jl index 1c713e3d03..d3b45e3294 100755 --- a/src/dataframe/dataframe.jl +++ b/src/dataframe/dataframe.jl @@ -635,7 +635,7 @@ Base.getindex(df::DataFrame, row_ind::typeof(!), col_inds::MultiColumnIndex) = function insert_single_column!(df::DataFrame, v::Any, col_ind::ColumnIndex; copycols = true) dv = _preprocess_column(v, nrow(df), copycols) if ncol(df) != 0 && nrow(df) != length(dv) - throw(ArgumentError("New columns must have the same length as old columns")) + throw(DimensionMismatch("Length of the column ($(length(dv))) and rows in the data frame ($(nrow(df))) are not equal")) end firstindex(dv) != 1 && _onebased_check_error() diff --git a/src/subdataframe/subdataframe.jl b/src/subdataframe/subdataframe.jl index 339209cb6b..63c6f6cda1 100644 --- a/src/subdataframe/subdataframe.jl +++ b/src/subdataframe/subdataframe.jl @@ -190,8 +190,7 @@ Base.@propagate_inbounds function Base.setindex!(sdf::SubDataFrame, val::Any, :: "columns of its parent data frame is disallowed")) end if !(val isa AbstractVector && nrow(sdf) == length(val)) - throw(ArgumentError("Assigned value must be a vector with length " * - "equal to number of rows in the SubDataFrame")) + throw(DimensionMismatch("Length of the assigned column ($(length(val))) and rows in the SubDataFrame ($(nrow(sdf))) are not equal")) end T = eltype(val) newcol = similar(val, Union{T, Missing}, nrow(parent(sdf))) diff --git a/test/broadcasting.jl b/test/broadcasting.jl index 7497b5a6fe..896250ff23 100644 --- a/test/broadcasting.jl +++ b/test/broadcasting.jl @@ -802,7 +802,7 @@ end @test df == cdf df = DataFrame(x=Int[]) - @test_throws ArgumentError df[!, :a] = sin.(1:3) + @test_throws DimensionMismatch df[!, :a] = sin.(1:3) df[!, :b] = sin.(1) df[!, :c] = sin(1) .+ 1 @test df == DataFrame(x=Int[], b=Float64[], c=Float64[]) @@ -910,7 +910,7 @@ end df = DataFrame(x=Union{Int,String}[]) df.x .= rhs if rhs isa AbstractVector && length(rhs) != 0 - @test_throws ArgumentError df.a = rhs + @test_throws DimensionMismatch df.a = rhs else df.a = rhs @test size(df) == (n, 2) @@ -993,7 +993,7 @@ end @test eltype(df.b) == Float64 df[!, :b] .= [1] @test eltype(df.b) == Float64 - @test_throws ArgumentError df[!, :b] = [1] + @test_throws DimensionMismatch df[!, :b] = [1] df[!, :b] = Int[] @test eltype(df.b) == Int df[!, :b] .= 'a' diff --git a/test/indexing.jl b/test/indexing.jl index f76e80d403..c554a73fac 100644 --- a/test/indexing.jl +++ b/test/indexing.jl @@ -1107,8 +1107,8 @@ end df = DataFrame(a=1:3, b=4:6, c=7:9) df[!, 1] = ["a", "b", "c"] @test df == DataFrame(a=["a", "b", "c"], b=4:6, c=7:9) - @test_throws ArgumentError df[!, 1] = ["a", "b"] - @test_throws ArgumentError df[!, 1] = ["a"] + @test_throws DimensionMismatch df[!, 1] = ["a", "b"] + @test_throws DimensionMismatch df[!, 1] = ["a"] @test_throws ArgumentError df[!, 5] = ["a", "b", "c"] df[!, :a] = 'a':'c' @test df == DataFrame(a='a':'c', b=4:6, c=7:9) @@ -1127,8 +1127,8 @@ end df = DataFrame(a=1:3, b=4:6, c=7:9) df[!, "a"] = ["a", "b", "c"] @test df == DataFrame(a=["a", "b", "c"], b=4:6, c=7:9) - @test_throws ArgumentError df[!, "a"] = ["a", "b"] - @test_throws ArgumentError df[!, "a"] = ["a"] + @test_throws DimensionMismatch df[!, "a"] = ["a", "b"] + @test_throws DimensionMismatch df[!, "a"] = ["a"] df[!, "a"] = 'a':'c' @test df == DataFrame(a='a':'c', b=4:6, c=7:9) df."a" = ["aaa", "bbb", 1] @@ -1580,7 +1580,7 @@ end df[!, :] = DataFrame(reshape(1:12, 3, :), :auto) @test df == DataFrame(reshape(1:12, 3, :), :auto) @test_throws ArgumentError df[!, :] = DataFrame(fill(1, 3, 4), :auto)[:, [3, 2, 1]] - @test_throws ArgumentError df[!, :] = DataFrame(fill(1, 3, 4), :auto)[1:2, :] + @test_throws DimensionMismatch df[!, :] = DataFrame(fill(1, 3, 4), :auto)[1:2, :] df = DataFrame(fill("x", 3, 4), :auto) df[!, Not(4)] = DataFrame(reshape(1:12, 3, :), :auto)[:, 1:3] diff --git a/test/select.jl b/test/select.jl index d7249ac9fc..45a5af6e0c 100644 --- a/test/select.jl +++ b/test/select.jl @@ -925,7 +925,7 @@ end DataFrame(x1=Char[], a=Int[]) end @test_throws ArgumentError select(df, [] => (() -> [9]) => :a, :) - @test_throws ArgumentError select(df, :, [] => (() -> [9]) => :a) + @test_throws DimensionMismatch select(df, :, [] => (() -> [9]) => :a) @test transform(df, names(df) .=> (x -> 9) .=> names(df)) == repeat(DataFrame([9 9 9], :auto), nrow(df)) @test combine(df, names(df) .=> (x -> 9) .=> names(df)) == @@ -1011,7 +1011,7 @@ end @test df2.x4_last isa CategoricalVector{Int} end - @test_throws ArgumentError select(df, names(df) .=> first, [] => (() -> Int[]) => :x1) + @test_throws DimensionMismatch select(df, names(df) .=> first, [] => (() -> Int[]) => :x1) df2 = combine(df, names(df) .=> first, [] => (() -> Int[]) => :x1) @test size(df2) == (0, 5) @test df2.x1_first isa Vector{Int} @@ -1019,7 +1019,7 @@ end @test df2.x3_first isa Vector{Missing} @test df2.x4_first isa Vector{Missing} - @test_throws ArgumentError select(df, names(df) .=> last, [] => (() -> Int[]) => :x1) + @test_throws DimensionMismatch select(df, names(df) .=> last, [] => (() -> Int[]) => :x1) df2 = combine(df, names(df) .=> last, [] => (() -> Int[]) => :x1) @test size(df2) == (0, 5) @test df2.x1_last isa Vector{Int} @@ -1273,8 +1273,8 @@ end @test df2.y === df.y @test transform(df, names(df) .=> first .=> names(df)) == DataFrame(x=fill(1, 3), y=fill(4, 3)) - @test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=true) - @test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=false) + @test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=true) + @test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=false) dfv = view(df, [2, 1], [2, 1]) @test select(dfv, :x => first) == DataFrame(x_first=fill(2, 2)) @@ -1292,8 +1292,8 @@ end @test_throws ArgumentError transform(dfv, :x => first, copycols=false) @test transform(dfv, names(dfv) .=> first .=> names(dfv)) == DataFrame(y=fill(5, 2), x=fill(2, 2)) - @test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=true) - @test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=false) + @test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=true) + @test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=false) end @testset "select! and transform! AbstractDataFrame" begin @@ -1327,7 +1327,7 @@ end @test df == DataFrame(x=fill(1, 3), y=fill(4, 3)) df = DataFrame(x=1:3, y=4:6) - @test_throws ArgumentError transform!(df, :x => x -> [1]) + @test_throws DimensionMismatch transform!(df, :x => x -> [1]) @test df == DataFrame(x=1:3, y=4:6) dfv = view(df, [2, 1], [2, 1]) @@ -1387,7 +1387,7 @@ end @test transform(sdf -> sdf.b, df) == [df DataFrame(x1=3:4)] @test transform(sdf -> (b = 2sdf.b,), df) == DataFrame(a=1:2, b=[6, 8], c=5:6) @test transform(sdf -> (b = 1,), df) == DataFrame(a=[1, 2], b=[1, 1], c=[5, 6]) - @test_throws ArgumentError transform(sdf -> (b = [1],), df) + @test_throws DimensionMismatch transform(sdf -> (b = [1],), df) @test transform(sdf -> (b = [1, 5],), df) == DataFrame(a=[1, 2], b=[1, 5], c=[5, 6]) @test transform(sdf -> 1, df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=1) @test transform(sdf -> fill([1]), df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[[1], [1]]) @@ -1397,8 +1397,8 @@ end for ret in (DataFrame(), NamedTuple(), zeros(0, 0), DataFrame(t=1)[1, 1:0]) @test transform(sdf -> ret, df) == df end - @test_throws ArgumentError transform(sdf -> DataFrame(a=10), df) - @test_throws ArgumentError transform(sdf -> zeros(1, 2), df) + @test_throws DimensionMismatch transform(sdf -> DataFrame(a=10), df) + @test_throws DimensionMismatch transform(sdf -> zeros(1, 2), df) @test transform(sdf -> DataFrame(a=[10, 11]), df) == DataFrame(a=[10, 11], b=3:4, c=5:6) @test transform(sdf -> [10 11; 12 13], df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[10, 12], x2=[11, 13]) @test transform(sdf -> DataFrame(a=10)[1, :], df) == DataFrame(a=[10, 10], b=3:4, c=5:6) @@ -1446,7 +1446,7 @@ end @test transform!(sdf -> sdf.b, copy(df)) == [df DataFrame(x1=3:4)] @test transform!(sdf -> (b = 2sdf.b,), copy(df)) == DataFrame(a=1:2, b=[6, 8], c=5:6) @test transform!(sdf -> (b = 1,), copy(df)) == DataFrame(a=[1, 2], b=[1, 1], c=[5, 6]) - @test_throws ArgumentError transform!(sdf -> (b = [1],), copy(df)) + @test_throws DimensionMismatch transform!(sdf -> (b = [1],), copy(df)) @test transform!(sdf -> (b = [1, 5],), copy(df)) == DataFrame(a=[1, 2], b=[1, 5], c=[5, 6]) @test transform!(sdf -> 1, copy(df)) == DataFrame(a=1:2, b=3:4, c=5:6, x1=1) @test transform!(sdf -> fill([1]), copy(df)) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[[1], [1]]) @@ -1456,8 +1456,8 @@ end for ret in (DataFrame(), NamedTuple(), zeros(0, 0), DataFrame(t=1)[1, 1:0]) @test transform!(sdf -> ret, copy(df)) == df end - @test_throws ArgumentError transform!(sdf -> DataFrame(a=10), copy(df)) - @test_throws ArgumentError transform!(sdf -> zeros(1, 2), copy(df)) + @test_throws DimensionMismatch transform!(sdf -> DataFrame(a=10), copy(df)) + @test_throws DimensionMismatch transform!(sdf -> zeros(1, 2), copy(df)) @test transform!(sdf -> DataFrame(a=[10, 11]), copy(df)) == DataFrame(a=[10, 11], b=3:4, c=5:6) @test transform!(sdf -> [10 11; 12 13], copy(df)) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[10, 12], x2=[11, 13]) @test transform!(sdf -> DataFrame(a=10)[1, :], copy(df)) == DataFrame(a=[10, 10], b=3:4, c=5:6) @@ -1489,7 +1489,7 @@ end @test combine(df, :a => (x -> res) => [:p, :q]) == DataFrame(p=1, q=2) @test_throws ArgumentError combine(df, :a => (x -> res) => [:p]) @test_throws ArgumentError select(df, :a => (x -> res) => AsTable) - @test_throws ArgumentError transform(df, :a => (x -> res) => AsTable) + @test_throws DimensionMismatch transform(df, :a => (x -> res) => AsTable) end @test combine(df, :a => ByRow(x -> [x, x+1]), :a => ByRow(x -> [x, x+1]) => AsTable, @@ -1625,8 +1625,8 @@ end DataFrame(a1=1, a2=2, a=1:2) @test select(df, :a => (x -> 1) => :a1, :a => (x -> 2) => :a2, [:a]) == DataFrame(a1=1, a2=2, a=1:2) - @test_throws ArgumentError combine(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a]) - @test_throws ArgumentError select(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a]) + @test_throws DimensionMismatch combine(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a]) + @test_throws DimensionMismatch select(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a]) end @testset "normalize_selection" begin diff --git a/test/subdataframe_mutation.jl b/test/subdataframe_mutation.jl index 9b81af9eec..74a506e3af 100644 --- a/test/subdataframe_mutation.jl +++ b/test/subdataframe_mutation.jl @@ -7,7 +7,7 @@ const ≅ = isequal @testset "mutating SubDataFrame with assignment to [!, col]" begin df = DataFrame() sdf = @view df[:, :] - @test_throws ArgumentError sdf[!, :a] = [1] + @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(a=[]) @@ -20,7 +20,7 @@ const ≅ = isequal df = DataFrame() sdf = @view df[1:0, :] - @test_throws ArgumentError sdf[!, :a] = [1] + @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(a=[]) @@ -33,7 +33,7 @@ const ≅ = isequal df = DataFrame(x=Int[]) sdf = @view df[:, :] - @test_throws ArgumentError sdf[!, :a] = [1] + @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(x=Int[], a=[]) @@ -52,7 +52,7 @@ const ≅ = isequal df = DataFrame(x=Int[]) sdf = @view df[1:0, :] - @test_throws ArgumentError sdf[!, :a] = [1] + @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(x=Int[], a=[]) @@ -71,7 +71,7 @@ const ≅ = isequal df = DataFrame(x=1:5) sdf = @view df[1:0, :] - @test_throws ArgumentError sdf[!, :a] = [1] + @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df ≅ DataFrame(x=1:5, a=missing) @@ -92,7 +92,7 @@ const ≅ = isequal df = DataFrame(x=1:5) sdf = @view df[:, :] - @test_throws ArgumentError sdf[!, :a] = [1] + @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :a] = 11:15 @test df.a isa Vector{Union{Missing, Int}} @test df ≅ DataFrame(x=1:5, a=11:15) @@ -158,7 +158,7 @@ const ≅ = isequal c=21:25, d=[missing, 102, 103, missing, missing], e=[missing, 1002, 1003, missing, missing]) - @test_throws ArgumentError sdf[!, :x] = [1] + @test_throws DimensionMismatch sdf[!, :x] = [1] @test_throws DimensionMismatch sdf[!, :a] = [1] sdf[!, :f] = categorical(["3", "2"]) @test df.f isa CategoricalArray @@ -239,7 +239,7 @@ end sdf[!, :b] .= 1 @test df.b isa Vector{Union{Missing, Int}} @test isempty(df.b) - @test_throws ArgumentError sdf[!, :c] = 1:2 + @test_throws DimensionMismatch sdf[!, :c] = 1:2 @test_throws DimensionMismatch sdf[!, :c] .= 1:2 @test_throws DimensionMismatch sdf[!, :a] .= 1:2 sdf[!, :a] .= [1.0] @@ -269,7 +269,7 @@ end sdf[!, :b] .= 1 @test df.b isa Vector{Union{Missing, Int}} @test isempty(df.b) - @test_throws ArgumentError sdf[!, :c] = 1:2 + @test_throws DimensionMismatch sdf[!, :c] = 1:2 @test_throws DimensionMismatch sdf[!, :c] .= 1:2 @test_throws DimensionMismatch sdf[!, :a] .= 1:2 sdf[!, :a] .= [1.0] @@ -699,7 +699,7 @@ end @testset "mutating SubDataFrame with assignment to [:, col]" begin df = DataFrame() sdf = @view df[:, :] - @test_throws ArgumentError sdf[:, :a] = [1] + @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(a=[]) @@ -712,7 +712,7 @@ end df = DataFrame() sdf = @view df[1:0, :] - @test_throws ArgumentError sdf[:, :a] = [1] + @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(a=[]) @@ -725,7 +725,7 @@ end df = DataFrame(x=Int[]) sdf = @view df[:, :] - @test_throws ArgumentError sdf[:, :a] = [1] + @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(x=Int[], a=[]) @@ -744,7 +744,7 @@ end df = DataFrame(x=Int[]) sdf = @view df[1:0, :] - @test_throws ArgumentError sdf[:, :a] = [1] + @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df == DataFrame(x=Int[], a=[]) @@ -763,7 +763,7 @@ end df = DataFrame(x=1:5) sdf = @view df[1:0, :] - @test_throws ArgumentError sdf[:, :a] = [1] + @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :a] = Int[] @test df.a isa Vector{Union{Missing, Int}} @test df ≅ DataFrame(x=1:5, a=missing) @@ -784,7 +784,7 @@ end df = DataFrame(x=1:5) sdf = @view df[:, :] - @test_throws ArgumentError sdf[:, :a] = [1] + @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :a] = 11:15 @test df.a isa Vector{Union{Missing, Int}} @test df ≅ DataFrame(x=1:5, a=11:15) @@ -848,8 +848,8 @@ end c=21:25, d=[missing, 102, 103, missing, missing], e=[missing, 1002, 1003, missing, missing]) - @test_throws ArgumentError sdf[:, :x] = 1 - @test_throws ArgumentError sdf[:, :x] = [1] + @test_throws DimensionMismatch sdf[:, :x] = 1 + @test_throws DimensionMismatch sdf[:, :x] = [1] @test_throws MethodError sdf[:, :a] = 1 @test_throws DimensionMismatch sdf[:, :a] = [1] sdf[:, :f] = categorical(["3", "2"]) @@ -913,7 +913,7 @@ end c=[21, 22, 33, 24, 25]) sdf = @view df[[3, 2], 1:2] - @test_throws ArgumentError df[!, :c] = 1:2 + @test_throws DimensionMismatch df[!, :c] = 1:2 end @testset "mutating SubDataFrame with broadcasting assignment to [:, col]" begin