From 0d1588aabbb8678ad42266460d41eb9645e01d61 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sun, 2 Aug 2020 01:21:30 -0600 Subject: [PATCH] Add check if input is a table before trying alternative constructors --- src/other/tables.jl | 10 +++++----- test/tables.jl | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/other/tables.jl b/src/other/tables.jl index bd4e4ee705..5ccd5ca88f 100644 --- a/src/other/tables.jl +++ b/src/other/tables.jl @@ -31,11 +31,11 @@ fromcolumns(x, names; copycols::Bool=true) = copycols=copycols) function DataFrame(x::T; copycols::Bool=true) where {T} - if x isa AbstractVector && all(col -> isa(col, AbstractVector), x) - return DataFrame(Vector{AbstractVector}(x), copycols=copycols) - end - if x isa AbstractVector || x isa Tuple - if all(v -> v isa Pair{Symbol, <:AbstractVector}, x) + if !Tables.istable(x) + if x isa AbstractVector && all(col -> isa(col, AbstractVector), x) + return DataFrame(Vector{AbstractVector}(x), copycols=copycols) + elseif (x isa AbstractVector || x isa Tuple) && + all(v -> v isa Pair{Symbol, <:AbstractVector}, x) return DataFrame(AbstractVector[last(v) for v in x], [first(v) for v in x], copycols=copycols) end diff --git a/test/tables.jl b/test/tables.jl index e57b081d41..7660ad76f8 100644 --- a/test/tables.jl +++ b/test/tables.jl @@ -53,6 +53,15 @@ Tables.schema(x::DuplicateNamesColumnTable) = Tables.Schema((:a, :a, :b), Tuple{ Base.getproperty(d::DuplicateNamesColumnTable, nm::Symbol) = [1.0, 2.0, 3.0] Base.propertynames(d::DuplicateNamesColumnTable) = (:a, :a, :b) +struct EmptyTableWithNames <: AbstractVector{Any} +end +Tables.isrowtable(::Type{EmptyTableWithNames}) = true +Tables.rows(x::EmptyTableWithNames) = x +Tables.schema(x::EmptyTableWithNames) = Tables.Schema((:a, :b, :c), Tuple{Float64, String, Float64}) +Base.size(x::EmptyTableWithNames) = (0,) +Base.eltype(x::EmptyTableWithNames) = NamedTuple +Base.iterate(x::EmptyTableWithNames, st=1) = nothing + @testset "Tables" begin df = DataFrame(a=Int64[1, 2, 3], b=[:a, :b, :c]) @@ -159,6 +168,12 @@ Base.propertynames(d::DuplicateNamesColumnTable) = (:a, :a, :b) @test ct.b !== cat2 @test ct.a == cat @test ct.b == cat == cat2 + + # https://github.com/JuliaData/CSV.jl/issues/702 + df = DataFrame(EmptyTableWithNames()) + @test size(df) == (0, 3) + @test names(df) == ["a", "b", "c"] + @test eltype.(eachcol(df)) == [Float64, String, Float64] end end