Skip to content

Commit 3b7b784

Browse files
committed
DimensionMismatch instead of ArgumentError on new columns with a different length
1 parent 4beab03 commit 3b7b784

6 files changed

Lines changed: 45 additions & 46 deletions

File tree

src/dataframe/dataframe.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ Base.getindex(df::DataFrame, row_ind::typeof(!), col_inds::MultiColumnIndex) =
635635
function insert_single_column!(df::DataFrame, v::Any, col_ind::ColumnIndex; copycols = true)
636636
dv = _preprocess_column(v, nrow(df), copycols)
637637
if ncol(df) != 0 && nrow(df) != length(dv)
638-
throw(ArgumentError("New columns must have the same length as old columns"))
638+
throw(DimensionMismatch("Length of the column ($(length(dv))) and rows in the data frame ($(nrow(df))) are not equal"))
639639
end
640640
firstindex(dv) != 1 && _onebased_check_error()
641641

src/subdataframe/subdataframe.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ Base.@propagate_inbounds function Base.setindex!(sdf::SubDataFrame, val::Any, ::
190190
"columns of its parent data frame is disallowed"))
191191
end
192192
if !(val isa AbstractVector && nrow(sdf) == length(val))
193-
throw(ArgumentError("Assigned value must be a vector with length " *
194-
"equal to number of rows in the SubDataFrame"))
193+
throw(DimensionMismatch("Length of the assigned column ($(length(val))) and rows in the SubDataFrame ($(nrow(sdf))) are not equal"))
195194
end
196195
T = eltype(val)
197196
newcol = similar(val, Union{T, Missing}, nrow(parent(sdf)))

test/broadcasting.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ end
803803
@test df == cdf
804804

805805
df = DataFrame(x=Int[])
806-
@test_throws ArgumentError df[!, :a] = sin.(1:3)
806+
@test_throws DimensionMismatch df[!, :a] = sin.(1:3)
807807
df[!, :b] = sin.(1)
808808
df[!, :c] = sin(1) .+ 1
809809
@test df == DataFrame(x=Int[], b=Float64[], c=Float64[])
@@ -911,7 +911,7 @@ end
911911
df = DataFrame(x=Union{Int,String}[])
912912
df.x .= rhs
913913
if rhs isa AbstractVector && length(rhs) != 0
914-
@test_throws ArgumentError df.a = rhs
914+
@test_throws DimensionMismatch df.a = rhs
915915
else
916916
df.a = rhs
917917
@test size(df) == (n, 2)
@@ -994,7 +994,7 @@ end
994994
@test eltype(df.b) == Float64
995995
df[!, :b] .= [1]
996996
@test eltype(df.b) == Float64
997-
@test_throws ArgumentError df[!, :b] = [1]
997+
@test_throws DimensionMismatch df[!, :b] = [1]
998998
df[!, :b] = Int[]
999999
@test eltype(df.b) == Int
10001000
df[!, :b] .= 'a'

test/indexing.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,8 +1108,8 @@ end
11081108
df = DataFrame(a=1:3, b=4:6, c=7:9)
11091109
df[!, 1] = ["a", "b", "c"]
11101110
@test df == DataFrame(a=["a", "b", "c"], b=4:6, c=7:9)
1111-
@test_throws ArgumentError df[!, 1] = ["a", "b"]
1112-
@test_throws ArgumentError df[!, 1] = ["a"]
1111+
@test_throws DimensionMismatch df[!, 1] = ["a", "b"]
1112+
@test_throws DimensionMismatch df[!, 1] = ["a"]
11131113
@test_throws ArgumentError df[!, 5] = ["a", "b", "c"]
11141114
df[!, :a] = 'a':'c'
11151115
@test df == DataFrame(a='a':'c', b=4:6, c=7:9)
@@ -1128,8 +1128,8 @@ end
11281128
df = DataFrame(a=1:3, b=4:6, c=7:9)
11291129
df[!, "a"] = ["a", "b", "c"]
11301130
@test df == DataFrame(a=["a", "b", "c"], b=4:6, c=7:9)
1131-
@test_throws ArgumentError df[!, "a"] = ["a", "b"]
1132-
@test_throws ArgumentError df[!, "a"] = ["a"]
1131+
@test_throws DimensionMismatch df[!, "a"] = ["a", "b"]
1132+
@test_throws DimensionMismatch df[!, "a"] = ["a"]
11331133
df[!, "a"] = 'a':'c'
11341134
@test df == DataFrame(a='a':'c', b=4:6, c=7:9)
11351135
df."a" = ["aaa", "bbb", 1]
@@ -1581,7 +1581,7 @@ end
15811581
df[!, :] = DataFrame(reshape(1:12, 3, :), :auto)
15821582
@test df == DataFrame(reshape(1:12, 3, :), :auto)
15831583
@test_throws ArgumentError df[!, :] = DataFrame(fill(1, 3, 4), :auto)[:, [3, 2, 1]]
1584-
@test_throws ArgumentError df[!, :] = DataFrame(fill(1, 3, 4), :auto)[1:2, :]
1584+
@test_throws DimensionMismatch df[!, :] = DataFrame(fill(1, 3, 4), :auto)[1:2, :]
15851585

15861586
df = DataFrame(fill("x", 3, 4), :auto)
15871587
df[!, Not(4)] = DataFrame(reshape(1:12, 3, :), :auto)[:, 1:3]

test/select.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ end
926926
DataFrame(x1=Char[], a=Int[])
927927
end
928928
@test_throws ArgumentError select(df, [] => (() -> [9]) => :a, :)
929-
@test_throws ArgumentError select(df, :, [] => (() -> [9]) => :a)
929+
@test_throws DimensionMismatch select(df, :, [] => (() -> [9]) => :a)
930930
@test transform(df, names(df) .=> (x -> 9) .=> names(df)) ==
931931
repeat(DataFrame([9 9 9], :auto), nrow(df))
932932
@test combine(df, names(df) .=> (x -> 9) .=> names(df)) ==
@@ -1012,15 +1012,15 @@ end
10121012
@test df2.x4_last isa CategoricalVector{Int}
10131013
end
10141014

1015-
@test_throws ArgumentError select(df, names(df) .=> first, [] => (() -> Int[]) => :x1)
1015+
@test_throws DimensionMismatch select(df, names(df) .=> first, [] => (() -> Int[]) => :x1)
10161016
df2 = combine(df, names(df) .=> first, [] => (() -> Int[]) => :x1)
10171017
@test size(df2) == (0, 5)
10181018
@test df2.x1_first isa Vector{Int}
10191019
@test df2.x2_first isa CategoricalVector{Int}
10201020
@test df2.x3_first isa Vector{Missing}
10211021
@test df2.x4_first isa Vector{Missing}
10221022

1023-
@test_throws ArgumentError select(df, names(df) .=> last, [] => (() -> Int[]) => :x1)
1023+
@test_throws DimensionMismatch select(df, names(df) .=> last, [] => (() -> Int[]) => :x1)
10241024
df2 = combine(df, names(df) .=> last, [] => (() -> Int[]) => :x1)
10251025
@test size(df2) == (0, 5)
10261026
@test df2.x1_last isa Vector{Int}
@@ -1274,8 +1274,8 @@ end
12741274
@test df2.y === df.y
12751275
@test transform(df, names(df) .=> first .=> names(df)) ==
12761276
DataFrame(x=fill(1, 3), y=fill(4, 3))
1277-
@test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=true)
1278-
@test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=false)
1277+
@test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=true)
1278+
@test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=false)
12791279

12801280
dfv = view(df, [2, 1], [2, 1])
12811281
@test select(dfv, :x => first) == DataFrame(x_first=fill(2, 2))
@@ -1293,8 +1293,8 @@ end
12931293
@test_throws ArgumentError transform(dfv, :x => first, copycols=false)
12941294
@test transform(dfv, names(dfv) .=> first .=> names(dfv)) ==
12951295
DataFrame(y=fill(5, 2), x=fill(2, 2))
1296-
@test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=true)
1297-
@test_throws ArgumentError transform(df, :x => x -> [first(x)], copycols=false)
1296+
@test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=true)
1297+
@test_throws DimensionMismatch transform(df, :x => x -> [first(x)], copycols=false)
12981298
end
12991299

13001300
@testset "select! and transform! AbstractDataFrame" begin
@@ -1328,7 +1328,7 @@ end
13281328
@test df == DataFrame(x=fill(1, 3), y=fill(4, 3))
13291329

13301330
df = DataFrame(x=1:3, y=4:6)
1331-
@test_throws ArgumentError transform!(df, :x => x -> [1])
1331+
@test_throws DimensionMismatch transform!(df, :x => x -> [1])
13321332
@test df == DataFrame(x=1:3, y=4:6)
13331333

13341334
dfv = view(df, [2, 1], [2, 1])
@@ -1388,7 +1388,7 @@ end
13881388
@test transform(sdf -> sdf.b, df) == [df DataFrame(x1=3:4)]
13891389
@test transform(sdf -> (b = 2sdf.b,), df) == DataFrame(a=1:2, b=[6, 8], c=5:6)
13901390
@test transform(sdf -> (b = 1,), df) == DataFrame(a=[1, 2], b=[1, 1], c=[5, 6])
1391-
@test_throws ArgumentError transform(sdf -> (b = [1],), df)
1391+
@test_throws DimensionMismatch transform(sdf -> (b = [1],), df)
13921392
@test transform(sdf -> (b = [1, 5],), df) == DataFrame(a=[1, 2], b=[1, 5], c=[5, 6])
13931393
@test transform(sdf -> 1, df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=1)
13941394
@test transform(sdf -> fill([1]), df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[[1], [1]])
@@ -1398,8 +1398,8 @@ end
13981398
for ret in (DataFrame(), NamedTuple(), zeros(0, 0), DataFrame(t=1)[1, 1:0])
13991399
@test transform(sdf -> ret, df) == df
14001400
end
1401-
@test_throws ArgumentError transform(sdf -> DataFrame(a=10), df)
1402-
@test_throws ArgumentError transform(sdf -> zeros(1, 2), df)
1401+
@test_throws DimensionMismatch transform(sdf -> DataFrame(a=10), df)
1402+
@test_throws DimensionMismatch transform(sdf -> zeros(1, 2), df)
14031403
@test transform(sdf -> DataFrame(a=[10, 11]), df) == DataFrame(a=[10, 11], b=3:4, c=5:6)
14041404
@test transform(sdf -> [10 11; 12 13], df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[10, 12], x2=[11, 13])
14051405
@test transform(sdf -> DataFrame(a=10)[1, :], df) == DataFrame(a=[10, 10], b=3:4, c=5:6)
@@ -1447,7 +1447,7 @@ end
14471447
@test transform!(sdf -> sdf.b, copy(df)) == [df DataFrame(x1=3:4)]
14481448
@test transform!(sdf -> (b = 2sdf.b,), copy(df)) == DataFrame(a=1:2, b=[6, 8], c=5:6)
14491449
@test transform!(sdf -> (b = 1,), copy(df)) == DataFrame(a=[1, 2], b=[1, 1], c=[5, 6])
1450-
@test_throws ArgumentError transform!(sdf -> (b = [1],), copy(df))
1450+
@test_throws DimensionMismatch transform!(sdf -> (b = [1],), copy(df))
14511451
@test transform!(sdf -> (b = [1, 5],), copy(df)) == DataFrame(a=[1, 2], b=[1, 5], c=[5, 6])
14521452
@test transform!(sdf -> 1, copy(df)) == DataFrame(a=1:2, b=3:4, c=5:6, x1=1)
14531453
@test transform!(sdf -> fill([1]), copy(df)) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[[1], [1]])
@@ -1457,8 +1457,8 @@ end
14571457
for ret in (DataFrame(), NamedTuple(), zeros(0, 0), DataFrame(t=1)[1, 1:0])
14581458
@test transform!(sdf -> ret, copy(df)) == df
14591459
end
1460-
@test_throws ArgumentError transform!(sdf -> DataFrame(a=10), copy(df))
1461-
@test_throws ArgumentError transform!(sdf -> zeros(1, 2), copy(df))
1460+
@test_throws DimensionMismatch transform!(sdf -> DataFrame(a=10), copy(df))
1461+
@test_throws DimensionMismatch transform!(sdf -> zeros(1, 2), copy(df))
14621462
@test transform!(sdf -> DataFrame(a=[10, 11]), copy(df)) == DataFrame(a=[10, 11], b=3:4, c=5:6)
14631463
@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])
14641464
@test transform!(sdf -> DataFrame(a=10)[1, :], copy(df)) == DataFrame(a=[10, 10], b=3:4, c=5:6)
@@ -1490,7 +1490,7 @@ end
14901490
@test combine(df, :a => (x -> res) => [:p, :q]) == DataFrame(p=1, q=2)
14911491
@test_throws ArgumentError combine(df, :a => (x -> res) => [:p])
14921492
@test_throws ArgumentError select(df, :a => (x -> res) => AsTable)
1493-
@test_throws ArgumentError transform(df, :a => (x -> res) => AsTable)
1493+
@test_throws DimensionMismatch transform(df, :a => (x -> res) => AsTable)
14941494
end
14951495
@test combine(df, :a => ByRow(x -> [x, x+1]),
14961496
:a => ByRow(x -> [x, x+1]) => AsTable,
@@ -1626,8 +1626,8 @@ end
16261626
DataFrame(a1=1, a2=2, a=1:2)
16271627
@test select(df, :a => (x -> 1) => :a1, :a => (x -> 2) => :a2, [:a]) ==
16281628
DataFrame(a1=1, a2=2, a=1:2)
1629-
@test_throws ArgumentError combine(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
1630-
@test_throws ArgumentError select(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
1629+
@test_throws DimensionMismatch combine(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
1630+
@test_throws DimensionMismatch select(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
16311631
end
16321632

16331633
@testset "normalize_selection" begin

test/subdataframe_mutation.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const ≅ = isequal
77
@testset "mutating SubDataFrame with assignment to [!, col]" begin
88
df = DataFrame()
99
sdf = @view df[:, :]
10-
@test_throws ArgumentError sdf[!, :a] = [1]
10+
@test_throws DimensionMismatch sdf[!, :a] = [1]
1111
sdf[!, :a] = Int[]
1212
@test df.a isa Vector{Union{Missing, Int}}
1313
@test df == DataFrame(a=[])
@@ -20,7 +20,7 @@ const ≅ = isequal
2020

2121
df = DataFrame()
2222
sdf = @view df[1:0, :]
23-
@test_throws ArgumentError sdf[!, :a] = [1]
23+
@test_throws DimensionMismatch sdf[!, :a] = [1]
2424
sdf[!, :a] = Int[]
2525
@test df.a isa Vector{Union{Missing, Int}}
2626
@test df == DataFrame(a=[])
@@ -33,7 +33,7 @@ const ≅ = isequal
3333

3434
df = DataFrame(x=Int[])
3535
sdf = @view df[:, :]
36-
@test_throws ArgumentError sdf[!, :a] = [1]
36+
@test_throws DimensionMismatch sdf[!, :a] = [1]
3737
sdf[!, :a] = Int[]
3838
@test df.a isa Vector{Union{Missing, Int}}
3939
@test df == DataFrame(x=Int[], a=[])
@@ -52,7 +52,7 @@ const ≅ = isequal
5252

5353
df = DataFrame(x=Int[])
5454
sdf = @view df[1:0, :]
55-
@test_throws ArgumentError sdf[!, :a] = [1]
55+
@test_throws DimensionMismatch sdf[!, :a] = [1]
5656
sdf[!, :a] = Int[]
5757
@test df.a isa Vector{Union{Missing, Int}}
5858
@test df == DataFrame(x=Int[], a=[])
@@ -71,7 +71,7 @@ const ≅ = isequal
7171

7272
df = DataFrame(x=1:5)
7373
sdf = @view df[1:0, :]
74-
@test_throws ArgumentError sdf[!, :a] = [1]
74+
@test_throws DimensionMismatch sdf[!, :a] = [1]
7575
sdf[!, :a] = Int[]
7676
@test df.a isa Vector{Union{Missing, Int}}
7777
@test df DataFrame(x=1:5, a=missing)
@@ -92,7 +92,7 @@ const ≅ = isequal
9292

9393
df = DataFrame(x=1:5)
9494
sdf = @view df[:, :]
95-
@test_throws ArgumentError sdf[!, :a] = [1]
95+
@test_throws DimensionMismatch sdf[!, :a] = [1]
9696
sdf[!, :a] = 11:15
9797
@test df.a isa Vector{Union{Missing, Int}}
9898
@test df DataFrame(x=1:5, a=11:15)
@@ -158,7 +158,7 @@ const ≅ = isequal
158158
c=21:25,
159159
d=[missing, 102, 103, missing, missing],
160160
e=[missing, 1002, 1003, missing, missing])
161-
@test_throws ArgumentError sdf[!, :x] = [1]
161+
@test_throws DimensionMismatch sdf[!, :x] = [1]
162162
@test_throws DimensionMismatch sdf[!, :a] = [1]
163163
sdf[!, :f] = categorical(["3", "2"])
164164
@test df.f isa CategoricalArray
@@ -239,7 +239,7 @@ end
239239
sdf[!, :b] .= 1
240240
@test df.b isa Vector{Union{Missing, Int}}
241241
@test isempty(df.b)
242-
@test_throws ArgumentError sdf[!, :c] = 1:2
242+
@test_throws DimensionMismatch sdf[!, :c] = 1:2
243243
@test_throws DimensionMismatch sdf[!, :c] .= 1:2
244244
@test_throws DimensionMismatch sdf[!, :a] .= 1:2
245245
sdf[!, :a] .= [1.0]
@@ -269,7 +269,7 @@ end
269269
sdf[!, :b] .= 1
270270
@test df.b isa Vector{Union{Missing, Int}}
271271
@test isempty(df.b)
272-
@test_throws ArgumentError sdf[!, :c] = 1:2
272+
@test_throws DimensionMismatch sdf[!, :c] = 1:2
273273
@test_throws DimensionMismatch sdf[!, :c] .= 1:2
274274
@test_throws DimensionMismatch sdf[!, :a] .= 1:2
275275
sdf[!, :a] .= [1.0]
@@ -699,7 +699,7 @@ end
699699
@testset "mutating SubDataFrame with assignment to [:, col]" begin
700700
df = DataFrame()
701701
sdf = @view df[:, :]
702-
@test_throws ArgumentError sdf[:, :a] = [1]
702+
@test_throws DimensionMismatch sdf[:, :a] = [1]
703703
sdf[:, :a] = Int[]
704704
@test df.a isa Vector{Union{Missing, Int}}
705705
@test df == DataFrame(a=[])
@@ -712,7 +712,7 @@ end
712712

713713
df = DataFrame()
714714
sdf = @view df[1:0, :]
715-
@test_throws ArgumentError sdf[:, :a] = [1]
715+
@test_throws DimensionMismatch sdf[:, :a] = [1]
716716
sdf[:, :a] = Int[]
717717
@test df.a isa Vector{Union{Missing, Int}}
718718
@test df == DataFrame(a=[])
@@ -725,7 +725,7 @@ end
725725

726726
df = DataFrame(x=Int[])
727727
sdf = @view df[:, :]
728-
@test_throws ArgumentError sdf[:, :a] = [1]
728+
@test_throws DimensionMismatch sdf[:, :a] = [1]
729729
sdf[:, :a] = Int[]
730730
@test df.a isa Vector{Union{Missing, Int}}
731731
@test df == DataFrame(x=Int[], a=[])
@@ -744,7 +744,7 @@ end
744744

745745
df = DataFrame(x=Int[])
746746
sdf = @view df[1:0, :]
747-
@test_throws ArgumentError sdf[:, :a] = [1]
747+
@test_throws DimensionMismatch sdf[:, :a] = [1]
748748
sdf[:, :a] = Int[]
749749
@test df.a isa Vector{Union{Missing, Int}}
750750
@test df == DataFrame(x=Int[], a=[])
@@ -763,7 +763,7 @@ end
763763

764764
df = DataFrame(x=1:5)
765765
sdf = @view df[1:0, :]
766-
@test_throws ArgumentError sdf[:, :a] = [1]
766+
@test_throws DimensionMismatch sdf[:, :a] = [1]
767767
sdf[:, :a] = Int[]
768768
@test df.a isa Vector{Union{Missing, Int}}
769769
@test df DataFrame(x=1:5, a=missing)
@@ -784,7 +784,7 @@ end
784784

785785
df = DataFrame(x=1:5)
786786
sdf = @view df[:, :]
787-
@test_throws ArgumentError sdf[:, :a] = [1]
787+
@test_throws DimensionMismatch sdf[:, :a] = [1]
788788
sdf[:, :a] = 11:15
789789
@test df.a isa Vector{Union{Missing, Int}}
790790
@test df DataFrame(x=1:5, a=11:15)
@@ -848,8 +848,8 @@ end
848848
c=21:25,
849849
d=[missing, 102, 103, missing, missing],
850850
e=[missing, 1002, 1003, missing, missing])
851-
@test_throws ArgumentError sdf[:, :x] = 1
852-
@test_throws ArgumentError sdf[:, :x] = [1]
851+
@test_throws DimensionMismatch sdf[:, :x] = 1
852+
@test_throws DimensionMismatch sdf[:, :x] = [1]
853853
@test_throws MethodError sdf[:, :a] = 1
854854
@test_throws DimensionMismatch sdf[:, :a] = [1]
855855
sdf[:, :f] = categorical(["3", "2"])
@@ -913,7 +913,7 @@ end
913913
c=[21, 22, 33, 24, 25])
914914

915915
sdf = @view df[[3, 2], 1:2]
916-
@test_throws ArgumentError df[!, :c] = 1:2
916+
@test_throws DimensionMismatch df[!, :c] = 1:2
917917
end
918918

919919
@testset "mutating SubDataFrame with broadcasting assignment to [:, col]" begin

0 commit comments

Comments
 (0)