From cfc4984bef1e416e58863adbef9f4da9392d3ffd Mon Sep 17 00:00:00 2001 From: ExpandingMan Date: Thu, 21 Jan 2021 23:38:40 -0500 Subject: [PATCH] improved behavior of Tables.Columns (#226) * improved behavior of Tables.Columns * bumped version * increased version number * removed check that Tables.Column is being passed a table * fixed small issues caused by last commit --- Project.toml | 2 +- src/Tables.jl | 3 +++ test/runtests.jl | 33 +++++++++++++++++++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 7d76372..86536a8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Tables" uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" authors = ["quinnj "] -version = "1.2.2" +version = "1.3.0" [deps] DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" diff --git a/src/Tables.jl b/src/Tables.jl index a31819e..20b3008 100644 --- a/src/Tables.jl +++ b/src/Tables.jl @@ -245,6 +245,9 @@ end Columns(x::AbstractColumns) = x +# Columns can only wrap something that is a table, so we pass the schema through +schema(x::Columns) = schema(getx(x)) + const RorC2 = Union{Row, Columns} getx(x::RorC2) = getfield(x, :x) diff --git a/test/runtests.jl b/test/runtests.jl index a8b641b..adfcac1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -268,6 +268,13 @@ end @test Mt == permutedims(m) # 167 @test !Tables.istable(Matrix{Union{}}(undef, 2, 3)) + + # Tables.Columns roundtrip + X = [1 0 0; 0 1 0; 0 0 1] + X′ = Tables.matrix(Tables.Columns(Tables.table(X))) + @test X′ == X + # test for presence of superfluous wrapping + @test typeof(X′) == typeof(X) end import Base: == @@ -524,22 +531,22 @@ struct DummyRow <: Tables.AbstractRow end @test_throws ErrorException("`Tables.columnnames` must be specifically overloaded for DummyRow <: Union{AbstractRow, AbstractColumns}`") Tables.columnnames(r) end -struct Columns <: Tables.AbstractColumns +struct TestColumns <: Tables.AbstractColumns a::Vector{Int} b::Vector{Union{Float64, Missing}} c::Vector{String} end -Tables.getcolumn(r::Columns, i::Int) = getfield(r, i) -Tables.getcolumn(r::Columns, nm::Symbol) = getfield(r, nm) -Tables.getcolumn(r::Columns, ::Type{T}, i::Int, nm::Symbol) where {T} = getfield(r, i) -Tables.columnnames(r::Columns) = fieldnames(Columns) +Tables.getcolumn(r::TestColumns, i::Int) = getfield(r, i) +Tables.getcolumn(r::TestColumns, nm::Symbol) = getfield(r, nm) +Tables.getcolumn(r::TestColumns, ::Type{T}, i::Int, nm::Symbol) where {T} = getfield(r, i) +Tables.columnnames(r::TestColumns) = fieldnames(TestColumns) struct DummyCols <: Tables.AbstractColumns end @testset "AbstractColumns" begin - col = Columns([1, 2], [missing, 3.14], ["hey", "ho"]) + col = TestColumns([1, 2], [missing, 3.14], ["hey", "ho"]) @test Base.IteratorSize(typeof(col)) == Base.HasLength() @test length(col) == 3 @@ -572,6 +579,20 @@ struct DummyCols <: Tables.AbstractColumns end @test_throws ErrorException("`Tables.columnnames` must be specifically overloaded for DummyCols <: Union{AbstractRow, AbstractColumns}`") Tables.columnnames(c) end +@testset "Tables.Columns" begin + X = (A=[1,2], B=[im, -im], C=["kirk", "spock"]) + + Xc = Tables.Columns(X) + @test Tables.schema(X) == Tables.schema(Xc) + for i ∈ 1:3 + @test Tables.getcolumn(X, i) == Tables.getcolumn(Xc, i) + end + for i ∈ (:A, :B, :C) + @test Tables.getcolumn(X, i) == Tables.getcolumn(Xc, i) + end + @test Tables.columnnames(X) == Tables.columnnames(Xc) +end + struct IsRowTable rows::Vector{NamedTuple} end