Skip to content

Commit

Permalink
Update DictRows and DictColumns definition (#310)
Browse files Browse the repository at this point in the history
* Update DictRows and DictColumns definition

* Update Project.toml

* Update runtests.jl

* Update runtests.jl

* Update test/runtests.jl

* Update src/Tables.jl

Co-authored-by: Milan Bouchet-Valat <[email protected]>

* Update src/Tables.jl

Co-authored-by: Milan Bouchet-Valat <[email protected]>
  • Loading branch information
bkamins and nalimilan authored Oct 11, 2022
1 parent e48ec0d commit 2cb1399
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tables"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
authors = ["quinnj <[email protected]>"]
version = "1.9.0"
version = "1.10.0"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
12 changes: 6 additions & 6 deletions src/Tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ getcolumn(x::AbstractDict{Symbol}, nm::Symbol) = x[nm]
getcolumn(x::AbstractDict{Symbol}, ::Type{T}, i::Int, nm::Symbol) where {T} = x[nm]
columnnames(x::AbstractDict{Symbol}) = collect(keys(x))

getcolumn(x::AbstractDict{String}, i::Int) = x[String(columnnames(x)[i])]
getcolumn(x::AbstractDict{String}, nm::Symbol) = x[String(nm)]
getcolumn(x::AbstractDict{String}, ::Type{T}, i::Int, nm::Symbol) where {T} = x[String(nm)]
columnnames(x::AbstractDict{String}) = collect(Symbol(k) for k in keys(x))
getcolumn(x::AbstractDict{<:AbstractString}, i::Int) = x[String(columnnames(x)[i])]
getcolumn(x::AbstractDict{<:AbstractString}, nm::Symbol) = x[String(nm)]
getcolumn(x::AbstractDict{<:AbstractString}, ::Type{T}, i::Int, nm::Symbol) where {T} = x[String(nm)]
columnnames(x::AbstractDict{<:AbstractString}) = collect(Symbol(k) for k in keys(x))

# AbstractVector of Dicts for Tables.rows
const DictRows = AbstractVector{T} where {T <: Union{AbstractDict{String}, AbstractDict{Symbol}}}
const DictRows = AbstractVector{T} where {T <: Union{AbstractDict{<:AbstractString}, AbstractDict{Symbol}}}
isrowtable(::Type{<:DictRows}) = true
# DictRows doesn't naturally lend itself to the `Tables.schema` requirement
# we can't just look at the first row, because the types might change,
Expand All @@ -159,7 +159,7 @@ isrowtable(::Type{<:DictRows}) = true
schema(x::DictRows) = nothing

# Dict of AbstractVectors for Tables.columns
const DictColumns = AbstractDict{K, V} where {K <: Union{Symbol, String}, V <: AbstractVector}
const DictColumns = Union{<:AbstractDict{<:AbstractString, <:AbstractVector}, <:AbstractDict{Symbol, <:AbstractVector}}
istable(::Type{<:DictColumns}) = true
columnaccess(::Type{<:DictColumns}) = true
columns(x::DictColumns) = x
Expand Down
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,34 @@ end
@test_throws MethodError Tables.ByRow(identity)(1)
@test_throws MethodError Tables.ByRow(identity)([1 2])
end

@testset "istable on dictionaries" begin
@test Tables.istable(Dict{Union{String}, Vector})
@test Tables.istable(Dict{Union{Symbol}, Vector})
@test Tables.istable(Dict{Union{SubString}, Vector})
@test Tables.istable(Dict{Union{AbstractString}, Vector})
@test !Tables.istable(Dict{Union{String, Symbol}, Vector})
@test Tables.istable(Vector{Dict{Symbol}})
@test Tables.istable(Vector{Dict{String}})
@test Tables.istable(Vector{Dict{SubString}})
@test Tables.istable(Vector{Dict{AbstractString}})

@test Set(Tables.columnnames(Dict(:a=>1, :b=>2))) == Set([:a, :b])
@test Set(Tables.columnnames(Dict("a"=>1, "b"=>2))) == Set([:a, :b])
@test Set(Tables.columnnames(Dict("a"=>1, SubString("b")=>2))) == Set([:a, :b])
@test Set(Tables.columnnames(Dict(SubString("a")=>1, SubString("b")=>2))) == Set([:a, :b])

@test Tables.getcolumn(Dict(:a=>1, :b=>2), 1) == 1
@test Tables.getcolumn(Dict("a"=>1, "b"=>2), 1 + (Int === Int64)) == 1
@test Tables.getcolumn(Dict("a"=>1, SubString("b")=>2), 1 + (Int === Int64)) == 1
@test Tables.getcolumn(Dict(SubString("a")=>1, SubString("b")=>2), 1 + (Int === Int64)) == 1
@test Tables.getcolumn(Dict(:a=>1, :b=>2), :a) == 1
@test Tables.getcolumn(Dict("a"=>1, "b"=>2), :a) == 1
@test Tables.getcolumn(Dict("a"=>1, SubString("b")=>2), :a) == 1
@test Tables.getcolumn(Dict(SubString("a")=>1, SubString("b")=>2), :a) == 1
@test Tables.getcolumn(Dict(:a=>1, :b=>2), Int, 1, :a) == 1
@test Tables.getcolumn(Dict("a"=>1, "b"=>2), Int, 1, :a) == 1
@test Tables.getcolumn(Dict("a"=>1, SubString("b")=>2), Int, 1, :a) == 1
@test Tables.getcolumn(Dict(SubString("a")=>1, SubString("b")=>2), Int, 1, :a) == 1
end

2 comments on commit 2cb1399

@quinnj
Copy link
Member

@quinnj quinnj commented on 2cb1399 Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/69963

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.10.0 -m "<description of version>" 2cb13998e856692ed273c931b83477caf8b7b020
git push origin v1.10.0

Please sign in to comment.