Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vcat in case no data frames are passed #3081

Merged
merged 7 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/abstractdataframe/abstractdataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ end
function _vcat(dfs::AbstractVector{AbstractDataFrame};
cols::Union{Symbol, AbstractVector{Symbol},
AbstractVector{<:AbstractString}}=:setequal)

# note that DataFrame() objects are dropped from dfs before we call _vcat
bkamins marked this conversation as resolved.
Show resolved Hide resolved
if isempty(dfs)
cols isa Symbol && return DataFrame()
return DataFrame([col => Missing[] for col in cols])
Expand Down
33 changes: 24 additions & 9 deletions test/dataframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2052,7 +2052,7 @@ end
cols=:orderequal)
end

@testset "vcat with source and reduce" begin
@testset "vcat with source and reduce(vcat, ...)" begin
df1 = DataFrame(A=1:3, B=1:3)
df2 = DataFrame(A=4:6, B=4:6)
df3 = DataFrame(A=7:9, C=7:9)
Expand All @@ -2067,16 +2067,14 @@ end
C=[fill(missing, 6); 7:9],
source=[1, 1, 1, 2, 2, 2, 3, 3, 3])
res = vcat(df1, df2, df3, df4, cols=:union, source=col => categorical(-4:-1))
@test res ≅ DataFrame(A=1:9, B=[1:6; fill(missing, 3)],
C=[fill(missing, 6); 7:9],
source=[-4, -4, -4, -3, -3, -3, -2, -2, -2])
@test res.source isa CategoricalVector
@test isequal_coltyped(res, DataFrame(A=1:9, B=[1:6; fill(missing, 3)],
C=[fill(missing, 6); 7:9],
source=categorical([-4, -4, -4, -3, -3, -3, -2, -2, -2])))

res = reduce(vcat, [df1, df2, df3, df4], cols=:union, source=col => categorical(-4:-1))
@test res ≅ DataFrame(A=1:9, B=[1:6; fill(missing, 3)],
C=[fill(missing, 6); 7:9],
source=[-4, -4, -4, -3, -3, -3, -2, -2, -2])
@test res.source isa CategoricalVector
@test isequal_coltyped(res, DataFrame(A=1:9, B=[1:6; fill(missing, 3)],
C=[fill(missing, 6); 7:9],
source=categorical([-4, -4, -4, -3, -3, -3, -2, -2, -2])))

@test reduce(vcat, DataFrame[]) == DataFrame()
@test isequal_coltyped(reduce(vcat, DataFrame[], source=:src),
Expand All @@ -2095,6 +2093,23 @@ end
@test_throws TypeError reduce(vcat, [df1, df2, df3, df4], cols=:union, source=:a => 1)
@test_throws ArgumentError reduce(vcat, [df1, df2, df3, df4], cols=:union, source=:C)
@test_throws ArgumentError reduce(vcat, [df1, df2, df3, df4], cols=:union, source=:a => [1])

@test vcat(DataFrame(), DataFrame()) ==
reduce(vcat, [DataFrame(), DataFrame()]) ==
DataFrame()
@test isequal_coltyped(vcat(DataFrame(), DataFrame(), cols=[:a, :b]),
DataFrame(a=Missing[], b=Missing[]))
@test isequal_coltyped(reduce(vcat, (DataFrame(), DataFrame()), cols=[:a, :b]),
DataFrame(a=Missing[], b=Missing[]))
@test isequal_coltyped(vcat(DataFrame(a=1:2), DataFrame(), cols=[:a, :b]),
DataFrame(a=1:2, b=missing))
@test isequal_coltyped(reduce(vcat, (DataFrame(a=1:2), DataFrame()), cols=[:a, :b]),
DataFrame(a=1:2, b=missing))
@test vcat(DataFrame(a=1), DataFrame(b=2), cols=[:a]) ≅ DataFrame(a=[1, missing])
@test vcat(DataFrame(a=1), DataFrame(b=2), cols=[:b]) ≅ DataFrame(b=[missing, 2])
@test vcat(DataFrame(a=1), DataFrame(b=2), cols=Symbol[]) == DataFrame()
@test isequal_coltyped(vcat(DataFrame(a=1), DataFrame(b=2), cols=[:c]),
DataFrame(c=[missing, missing]))
end

@testset "push! with :subset" begin
Expand Down