Skip to content

Commit

Permalink
Test with DataFrames, fix df_append_columns!!
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Nov 4, 2019
1 parent f2936fc commit c099249
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/dataframes_impl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ function checkcolumnnames(x::Union{Tuple,AbstractVector}, columnnames)
length(columnnames) == length(x) || error("Number of columns does not match.")
end

function df_append_columns!!(df, columns)
function df_append_columns!!(df, table)
columns = getfield(df, :columns)
checkcolumnnames(columns, propertynames(df))
for (pos, (name, col)) in enumerate(zip(propertynames(df), columns))
columns[pos] = append!!(col, _getvalue(columns, pos, name))
colnames = DataFrames._names(df) # avoid copy
checkcolumnnames(columns, colnames)
for (pos, (name, col)) in enumerate(zip(colnames, columns))
columns[pos] = append!!(col, _getvalue(table, pos, name))
end
return df
end
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
BangBang = "198e06fe-97b7-11e9-32a5-e1d131e6ad66"
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand Down
67 changes: 67 additions & 0 deletions test/test_dataframes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module TestDataFrames

using BangBang: append!!, push!!
using CategoricalArrays: CategoricalArray
using DataFrames: DataFrame
using Test

@testset "push!!" begin
@testset "column: $(typeof(column)); row: $(typeof(row))" for (column, row) in [
([0], (a = 1,)),
([0], Dict(:a => 1)),
([0], [1]),
([0], (1,)),
([0.5], (a = 1,)),
([0.5], Dict(:a => 1)),
([0.5], [1]),
([0.5], (1,)),
([0], (a = 1.5,)),
(CategoricalArray(["A", "B"]), (a = "A",)),
(CategoricalArray(["A", "B"]), (a = "C",)),
]
df = DataFrame(a = copy(column))
# row isa DataFrame
# row[:, :a] .+= 1
if row isa Union{Array, Tuple}
df2 = DataFrame(a=row[1])
else
df2 = DataFrame([(; pairs(row)...)])
end
@test push!!(copy(df), row) == vcat(df, df2)
@test push!(push!!(copy(df), row), row) isa DataFrame
end
end

@testset "append!!" begin
@testset "column: $(typeof(column)); source: $(typeof(source))" for (
column,
source,
) in [
([0], ((a = 1,),)),
([0], [(a = 1,)]),
([0], (a = [1],)),
# ([0], Dict(:a => [1])),
([0], DataFrame(a = [1])),
([0.5], ((a = 1,),)),
([0.5], [(a = 1,)]),
([0.5], (a = [1],)),
([0.5], DataFrame(a = [1])),
([0], ((a = 1.5,),)),
([0], [(a = 1.5,)]),
([0], (a = [1.5],)),
([0], DataFrame(a = [1.5])),
(CategoricalArray(["A", "B"]), ((a = "A",),)),
(CategoricalArray(["A", "B"]), [(a = "A",)]),
(CategoricalArray(["A", "B"]), (a = ["A"],)),
(CategoricalArray(["A", "B"]), ((a = "C",),)),
(CategoricalArray(["A", "B"]), [(a = "C",)]),
(CategoricalArray(["A", "B"]), (a = ["C"],)),
]
df = DataFrame(a = copy(column))
# source isa DataFrame
# source[:, :a] .+= 1
@test append!!(copy(df), source) == vcat(df, DataFrame(source))
end
end

end # module

0 comments on commit c099249

Please sign in to comment.