diff --git a/src/dataframe/insertion.jl b/src/dataframe/insertion.jl index b790155b1..d04829499 100644 --- a/src/dataframe/insertion.jl +++ b/src/dataframe/insertion.jl @@ -1103,8 +1103,14 @@ end function Base.push!(df::DataFrame, @nospecialize rows...; cols::Symbol=:setequal, promote::Bool=(cols in [:union, :subset])) + if isempty(rows) + if !(cols in (:orderequal, :setequal, :intersect, :subset, :union)) + throw(ArgumentError("`cols` keyword argument must be " * + ":orderequal, :setequal, :intersect, :subset or :union)")) + end + end with_names_count = count(rows) do row - row isa Union{AbstractDict,NamedTuple,Tables.AbstractRow} + row isa Union{DataFrameRow,AbstractDict,NamedTuple,Tables.AbstractRow} end if 0 < with_names_count < length(rows) throw(ArgumentError("Mixing rows with column names and without column names " * @@ -1116,8 +1122,14 @@ end function Base.pushfirst!(df::DataFrame, @nospecialize rows...; cols::Symbol=:setequal, promote::Bool=(cols in [:union, :subset])) + if isempty(rows) + if !(cols in (:orderequal, :setequal, :intersect, :subset, :union)) + throw(ArgumentError("`cols` keyword argument must be " * + ":orderequal, :setequal, :intersect, :subset or :union)")) + end + end with_names_count = count(rows) do row - row isa Union{AbstractDict,NamedTuple,Tables.AbstractRow} + row isa Union{DataFrameRow,AbstractDict,NamedTuple,Tables.AbstractRow} end if 0 < with_names_count < length(rows) throw(ArgumentError("Mixing rows with column names and without column names " * diff --git a/test/insertion.jl b/test/insertion.jl index 8c60e2349..3e9bb7d5f 100644 --- a/test/insertion.jl +++ b/test/insertion.jl @@ -1394,9 +1394,9 @@ end DataFrame(a=[3, 5, 7, 1], b=[4, 6, 8, 2]) end - for x in (DataFrame(a=3, b=4)[1, :], (a=3, b=4), (3, 4)), - y in (DataFrame(a=5, b=6)[1, :], (a=5, b=6), (5, 6)), - z in (DataFrame(a=7, b=8)[1, :], (a=7, b=8), (7, 8)) + for x in (DataFrame(a=3, b=4)[1, :], (a=3, b=4)), + y in (DataFrame(a=5, b=6)[1, :], (a=5, b=6)), + z in (DataFrame(a=7, b=8)[1, :], (a=7, b=8)) @test push!(copy(df), x, y) == DataFrame(a=1:2:5, b=2:2:6) @test push!(copy(df), x, y, z) == @@ -1405,7 +1405,7 @@ end DataFrame(a=[3, 5, 1], b=[4, 6, 2]) @test pushfirst!(copy(df), x, y, z) == DataFrame(a=[3, 5, 7, 1], b=[4, 6, 8, 2]) - for cols in (:orderequal, :setequal) + for cols in (:orderequal, :setequal, :union, :subset, :intersect) @test push!(copy(df), x, y, cols=cols) == DataFrame(a=1:2:5, b=2:2:6) @test push!(copy(df), x, y, z, cols=cols) == @@ -1417,6 +1417,17 @@ end end end + for x in ((3, 4), [3, 4]), y in ((5, 6), [5, 6]), z in ((7, 8), [7, 8]) + @test push!(copy(df), x, y) == + DataFrame(a=1:2:5, b=2:2:6) + @test push!(copy(df), x, y, z) == + DataFrame(a=1:2:7, b=2:2:8) + @test pushfirst!(copy(df), x, y) == + DataFrame(a=[3, 5, 1], b=[4, 6, 2]) + @test pushfirst!(copy(df), x, y, z) == + DataFrame(a=[3, 5, 7, 1], b=[4, 6, 8, 2]) + end + for x in (DataFrame(a=3, b=4), (a=[3], b=[4]), [(a=3, b=4)]), y in (DataFrame(a=5, c=6), (a=[5], c=[6]), [(a=5, c=6)]), z in (DataFrame(a="7", d=8), (a=["7"], d=[8]), [(a="7", d=8)]) @@ -1457,9 +1468,9 @@ end @test_throws ArgumentError push!(df, (1, 2), (1, 2), cols=:union) @test_throws ArgumentError pushfirst!(df, (1, 2), (1, 2), cols=:union) - @test insert!(DataFrame(a=1:3, b=11:13), 2, (0, 10), cols=:orderequal) == + @test insert!(DataFrame(a=1:3, b=11:13), 2, (0, 10), cols=:setequal) == DataFrame(a=[1, 0, 2, 3], b=[11, 10, 12, 13]) - @test_throws ArgumentError insert!(df, 1, (1, 2), cols=:union) + @test_throws ArgumentError insert!(df, 1, (1, 2), cols=:orderequal) end end # module