Skip to content

Commit

Permalink
Add check if input is a table before trying alternative constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 2, 2020
1 parent 65ba853 commit 0d1588a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/other/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0d1588a

Please sign in to comment.