Skip to content

Commit

Permalink
DimensionMismatch instead of ArgumentError on new columns with a diff…
Browse files Browse the repository at this point in the history
…erent length
  • Loading branch information
gustafsson committed Oct 25, 2022
1 parent 7cdf7b1 commit 598f381
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/dataframe/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
3 changes: 1 addition & 2 deletions src/subdataframe/subdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
6 changes: 3 additions & 3 deletions test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'
Expand Down
10 changes: 5 additions & 5 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
34 changes: 17 additions & 17 deletions test/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ==
Expand Down Expand Up @@ -1011,15 +1011,15 @@ 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}
@test df2.x2_first isa CategoricalVector{Int}
@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}
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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]])
Expand All @@ -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)
Expand Down Expand Up @@ -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]])
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
36 changes: 18 additions & 18 deletions test/subdataframe_mutation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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=[])
Expand All @@ -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=[])
Expand All @@ -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=[])
Expand All @@ -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=[])
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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=[])
Expand All @@ -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=[])
Expand All @@ -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=[])
Expand All @@ -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=[])
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 598f381

Please sign in to comment.