Skip to content

Commit cfc4984

Browse files
authored
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
1 parent 36be703 commit cfc4984

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Tables"
22
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
33
authors = ["quinnj <[email protected]>"]
4-
version = "1.2.2"
4+
version = "1.3.0"
55

66
[deps]
77
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

src/Tables.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ end
245245

246246
Columns(x::AbstractColumns) = x
247247

248+
# Columns can only wrap something that is a table, so we pass the schema through
249+
schema(x::Columns) = schema(getx(x))
250+
248251
const RorC2 = Union{Row, Columns}
249252

250253
getx(x::RorC2) = getfield(x, :x)

test/runtests.jl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ end
268268
@test Mt == permutedims(m)
269269
# 167
270270
@test !Tables.istable(Matrix{Union{}}(undef, 2, 3))
271+
272+
# Tables.Columns roundtrip
273+
X = [1 0 0; 0 1 0; 0 0 1]
274+
X′ = Tables.matrix(Tables.Columns(Tables.table(X)))
275+
@test X′ == X
276+
# test for presence of superfluous wrapping
277+
@test typeof(X′) == typeof(X)
271278
end
272279

273280
import Base: ==
@@ -524,22 +531,22 @@ struct DummyRow <: Tables.AbstractRow end
524531
@test_throws ErrorException("`Tables.columnnames` must be specifically overloaded for DummyRow <: Union{AbstractRow, AbstractColumns}`") Tables.columnnames(r)
525532
end
526533

527-
struct Columns <: Tables.AbstractColumns
534+
struct TestColumns <: Tables.AbstractColumns
528535
a::Vector{Int}
529536
b::Vector{Union{Float64, Missing}}
530537
c::Vector{String}
531538
end
532539

533-
Tables.getcolumn(r::Columns, i::Int) = getfield(r, i)
534-
Tables.getcolumn(r::Columns, nm::Symbol) = getfield(r, nm)
535-
Tables.getcolumn(r::Columns, ::Type{T}, i::Int, nm::Symbol) where {T} = getfield(r, i)
536-
Tables.columnnames(r::Columns) = fieldnames(Columns)
540+
Tables.getcolumn(r::TestColumns, i::Int) = getfield(r, i)
541+
Tables.getcolumn(r::TestColumns, nm::Symbol) = getfield(r, nm)
542+
Tables.getcolumn(r::TestColumns, ::Type{T}, i::Int, nm::Symbol) where {T} = getfield(r, i)
543+
Tables.columnnames(r::TestColumns) = fieldnames(TestColumns)
537544

538545
struct DummyCols <: Tables.AbstractColumns end
539546

540547
@testset "AbstractColumns" begin
541548

542-
col = Columns([1, 2], [missing, 3.14], ["hey", "ho"])
549+
col = TestColumns([1, 2], [missing, 3.14], ["hey", "ho"])
543550

544551
@test Base.IteratorSize(typeof(col)) == Base.HasLength()
545552
@test length(col) == 3
@@ -572,6 +579,20 @@ struct DummyCols <: Tables.AbstractColumns end
572579
@test_throws ErrorException("`Tables.columnnames` must be specifically overloaded for DummyCols <: Union{AbstractRow, AbstractColumns}`") Tables.columnnames(c)
573580
end
574581

582+
@testset "Tables.Columns" begin
583+
X = (A=[1,2], B=[im, -im], C=["kirk", "spock"])
584+
585+
Xc = Tables.Columns(X)
586+
@test Tables.schema(X) == Tables.schema(Xc)
587+
for i 1:3
588+
@test Tables.getcolumn(X, i) == Tables.getcolumn(Xc, i)
589+
end
590+
for i (:A, :B, :C)
591+
@test Tables.getcolumn(X, i) == Tables.getcolumn(Xc, i)
592+
end
593+
@test Tables.columnnames(X) == Tables.columnnames(Xc)
594+
end
595+
575596
struct IsRowTable
576597
rows::Vector{NamedTuple}
577598
end

0 commit comments

Comments
 (0)