Skip to content

Commit e140904

Browse files
committed
DimensionMismatch instead of ArgumentError on new columns with a different length
1 parent 5352a63 commit e140904

File tree

6 files changed

+45
-46
lines changed

6 files changed

+45
-46
lines changed

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
@@ -802,7 +802,7 @@ end
802802
@test df == cdf
803803

804804
df = DataFrame(x=Int[])
805-
@test_throws ArgumentError df[!, :a] = sin.(1:3)
805+
@test_throws DimensionMismatch df[!, :a] = sin.(1:3)
806806
df[!, :b] = sin.(1)
807807
df[!, :c] = sin(1) .+ 1
808808
@test df == DataFrame(x=Int[], b=Float64[], c=Float64[])
@@ -910,7 +910,7 @@ end
910910
df = DataFrame(x=Union{Int,String}[])
911911
df.x .= rhs
912912
if rhs isa AbstractVector && length(rhs) != 0
913-
@test_throws ArgumentError df.a = rhs
913+
@test_throws DimensionMismatch df.a = rhs
914914
else
915915
df.a = rhs
916916
@test size(df) == (n, 2)
@@ -993,7 +993,7 @@ end
993993
@test eltype(df.b) == Float64
994994
df[!, :b] .= [1]
995995
@test eltype(df.b) == Float64
996-
@test_throws ArgumentError df[!, :b] = [1]
996+
@test_throws DimensionMismatch df[!, :b] = [1]
997997
df[!, :b] = Int[]
998998
@test eltype(df.b) == Int
999999
df[!, :b] .= 'a'

test/indexing.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,8 @@ end
11071107
df = DataFrame(a=1:3, b=4:6, c=7:9)
11081108
df[!, 1] = ["a", "b", "c"]
11091109
@test df == DataFrame(a=["a", "b", "c"], b=4:6, c=7:9)
1110-
@test_throws ArgumentError df[!, 1] = ["a", "b"]
1111-
@test_throws ArgumentError df[!, 1] = ["a"]
1110+
@test_throws DimensionMismatch df[!, 1] = ["a", "b"]
1111+
@test_throws DimensionMismatch df[!, 1] = ["a"]
11121112
@test_throws ArgumentError df[!, 5] = ["a", "b", "c"]
11131113
df[!, :a] = 'a':'c'
11141114
@test df == DataFrame(a='a':'c', b=4:6, c=7:9)
@@ -1127,8 +1127,8 @@ end
11271127
df = DataFrame(a=1:3, b=4:6, c=7:9)
11281128
df[!, "a"] = ["a", "b", "c"]
11291129
@test df == DataFrame(a=["a", "b", "c"], b=4:6, c=7:9)
1130-
@test_throws ArgumentError df[!, "a"] = ["a", "b"]
1131-
@test_throws ArgumentError df[!, "a"] = ["a"]
1130+
@test_throws DimensionMismatch df[!, "a"] = ["a", "b"]
1131+
@test_throws DimensionMismatch df[!, "a"] = ["a"]
11321132
df[!, "a"] = 'a':'c'
11331133
@test df == DataFrame(a='a':'c', b=4:6, c=7:9)
11341134
df."a" = ["aaa", "bbb", 1]
@@ -1580,7 +1580,7 @@ end
15801580
df[!, :] = DataFrame(reshape(1:12, 3, :), :auto)
15811581
@test df == DataFrame(reshape(1:12, 3, :), :auto)
15821582
@test_throws ArgumentError df[!, :] = DataFrame(fill(1, 3, 4), :auto)[:, [3, 2, 1]]
1583-
@test_throws ArgumentError df[!, :] = DataFrame(fill(1, 3, 4), :auto)[1:2, :]
1583+
@test_throws DimensionMismatch df[!, :] = DataFrame(fill(1, 3, 4), :auto)[1:2, :]
15841584

15851585
df = DataFrame(fill("x", 3, 4), :auto)
15861586
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
@@ -925,7 +925,7 @@ end
925925
DataFrame(x1=Char[], a=Int[])
926926
end
927927
@test_throws ArgumentError select(df, [] => (() -> [9]) => :a, :)
928-
@test_throws ArgumentError select(df, :, [] => (() -> [9]) => :a)
928+
@test_throws DimensionMismatch select(df, :, [] => (() -> [9]) => :a)
929929
@test transform(df, names(df) .=> (x -> 9) .=> names(df)) ==
930930
repeat(DataFrame([9 9 9], :auto), nrow(df))
931931
@test combine(df, names(df) .=> (x -> 9) .=> names(df)) ==
@@ -1011,15 +1011,15 @@ end
10111011
@test df2.x4_last isa CategoricalVector{Int}
10121012
end
10131013

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

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

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

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

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

13331333
dfv = view(df, [2, 1], [2, 1])
@@ -1387,7 +1387,7 @@ end
13871387
@test transform(sdf -> sdf.b, df) == [df DataFrame(x1=3:4)]
13881388
@test transform(sdf -> (b = 2sdf.b,), df) == DataFrame(a=1:2, b=[6, 8], c=5:6)
13891389
@test transform(sdf -> (b = 1,), df) == DataFrame(a=[1, 2], b=[1, 1], c=[5, 6])
1390-
@test_throws ArgumentError transform(sdf -> (b = [1],), df)
1390+
@test_throws DimensionMismatch transform(sdf -> (b = [1],), df)
13911391
@test transform(sdf -> (b = [1, 5],), df) == DataFrame(a=[1, 2], b=[1, 5], c=[5, 6])
13921392
@test transform(sdf -> 1, df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=1)
13931393
@test transform(sdf -> fill([1]), df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[[1], [1]])
@@ -1397,8 +1397,8 @@ end
13971397
for ret in (DataFrame(), NamedTuple(), zeros(0, 0), DataFrame(t=1)[1, 1:0])
13981398
@test transform(sdf -> ret, df) == df
13991399
end
1400-
@test_throws ArgumentError transform(sdf -> DataFrame(a=10), df)
1401-
@test_throws ArgumentError transform(sdf -> zeros(1, 2), df)
1400+
@test_throws DimensionMismatch transform(sdf -> DataFrame(a=10), df)
1401+
@test_throws DimensionMismatch transform(sdf -> zeros(1, 2), df)
14021402
@test transform(sdf -> DataFrame(a=[10, 11]), df) == DataFrame(a=[10, 11], b=3:4, c=5:6)
14031403
@test transform(sdf -> [10 11; 12 13], df) == DataFrame(a=1:2, b=3:4, c=5:6, x1=[10, 12], x2=[11, 13])
14041404
@test transform(sdf -> DataFrame(a=10)[1, :], df) == DataFrame(a=[10, 10], b=3:4, c=5:6)
@@ -1446,7 +1446,7 @@ end
14461446
@test transform!(sdf -> sdf.b, copy(df)) == [df DataFrame(x1=3:4)]
14471447
@test transform!(sdf -> (b = 2sdf.b,), copy(df)) == DataFrame(a=1:2, b=[6, 8], c=5:6)
14481448
@test transform!(sdf -> (b = 1,), copy(df)) == DataFrame(a=[1, 2], b=[1, 1], c=[5, 6])
1449-
@test_throws ArgumentError transform!(sdf -> (b = [1],), copy(df))
1449+
@test_throws DimensionMismatch transform!(sdf -> (b = [1],), copy(df))
14501450
@test transform!(sdf -> (b = [1, 5],), copy(df)) == DataFrame(a=[1, 2], b=[1, 5], c=[5, 6])
14511451
@test transform!(sdf -> 1, copy(df)) == DataFrame(a=1:2, b=3:4, c=5:6, x1=1)
14521452
@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
14561456
for ret in (DataFrame(), NamedTuple(), zeros(0, 0), DataFrame(t=1)[1, 1:0])
14571457
@test transform!(sdf -> ret, copy(df)) == df
14581458
end
1459-
@test_throws ArgumentError transform!(sdf -> DataFrame(a=10), copy(df))
1460-
@test_throws ArgumentError transform!(sdf -> zeros(1, 2), copy(df))
1459+
@test_throws DimensionMismatch transform!(sdf -> DataFrame(a=10), copy(df))
1460+
@test_throws DimensionMismatch transform!(sdf -> zeros(1, 2), copy(df))
14611461
@test transform!(sdf -> DataFrame(a=[10, 11]), copy(df)) == DataFrame(a=[10, 11], b=3:4, c=5:6)
14621462
@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])
14631463
@test transform!(sdf -> DataFrame(a=10)[1, :], copy(df)) == DataFrame(a=[10, 10], b=3:4, c=5:6)
@@ -1489,7 +1489,7 @@ end
14891489
@test combine(df, :a => (x -> res) => [:p, :q]) == DataFrame(p=1, q=2)
14901490
@test_throws ArgumentError combine(df, :a => (x -> res) => [:p])
14911491
@test_throws ArgumentError select(df, :a => (x -> res) => AsTable)
1492-
@test_throws ArgumentError transform(df, :a => (x -> res) => AsTable)
1492+
@test_throws DimensionMismatch transform(df, :a => (x -> res) => AsTable)
14931493
end
14941494
@test combine(df, :a => ByRow(x -> [x, x+1]),
14951495
:a => ByRow(x -> [x, x+1]) => AsTable,
@@ -1625,8 +1625,8 @@ end
16251625
DataFrame(a1=1, a2=2, a=1:2)
16261626
@test select(df, :a => (x -> 1) => :a1, :a => (x -> 2) => :a2, [:a]) ==
16271627
DataFrame(a1=1, a2=2, a=1:2)
1628-
@test_throws ArgumentError combine(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
1629-
@test_throws ArgumentError select(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
1628+
@test_throws DimensionMismatch combine(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
1629+
@test_throws DimensionMismatch select(df, :a => (x -> 1) => :a1, :a => (x -> [2]) => :a2, [:a])
16301630
end
16311631

16321632
@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)