Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Sep 25, 2023
1 parent 58c988d commit 269cc55
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/dataframe/insertion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 " *

Check warning on line 1116 in src/dataframe/insertion.jl

View check run for this annotation

Codecov / codecov/patch

src/dataframe/insertion.jl#L1116

Added line #L1116 was not covered by tests
Expand All @@ -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 " *

Check warning on line 1135 in src/dataframe/insertion.jl

View check run for this annotation

Codecov / codecov/patch

src/dataframe/insertion.jl#L1135

Added line #L1135 was not covered by tests
Expand Down
23 changes: 17 additions & 6 deletions test/insertion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ==
Expand All @@ -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) ==
Expand All @@ -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)])
Expand Down Expand Up @@ -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

0 comments on commit 269cc55

Please sign in to comment.