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

Add check if input is a table before trying alternative constructors #2341

Merged
merged 4 commits into from
Aug 2, 2020
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
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