From 7194d5c877b683d65c36761dfc0ac757f70b8665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Fri, 14 Dec 2018 11:52:45 +0100 Subject: [PATCH 1/6] DataFrameRow: fix iterate, ndims, iterate --- src/dataframerow/dataframerow.jl | 10 +++------- src/deprecated.jl | 3 --- test/dataframerow.jl | 20 +++++++++++++++++++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index b348769bb9..6c162d31ba 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -80,15 +80,11 @@ index(r::DataFrameRow) = index(parent(r)) Base.size(r::DataFrameRow) = (size(parent(r), 2),) Base.size(r::DataFrameRow, i) = size(r)[i] Base.length(r::DataFrameRow) = size(parent(r), 2) +Base.ndims(r::DataFrameRow) = 1 -Compat.lastindex(r::DataFrameRow) = size(parent(r), 2) +Base.lastindex(r::DataFrameRow) = size(parent(r), 2) -function Base.iterate(r::DataFrameRow) - Base.depwarn("iteration over DataFrameRow will return values in the future:" * - "use pairs(r::DataFrameRow) to get the current behavior", - :start) - iterate(r, 1) -end +Base.iterate(r::DataFrameRow) = iterate(r, 1) function Base.iterate(r::DataFrameRow, st) st > length(r) && return nothing diff --git a/src/deprecated.jl b/src/deprecated.jl index 8dfd11ee27..bfde491907 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -1328,9 +1328,6 @@ import Base: vcat @deprecate showcols(df::AbstractDataFrame, all::Bool=false, values::Bool=true) describe(df, stats = [:eltype, :nmissing, :first, :last]) @deprecate showcols(io::IO, df::AbstractDataFrame, all::Bool=false, values::Bool=true) show(io, describe(df, stats = [:eltype, :nmissing, :first, :last]), all) -import Base: collect -@deprecate collect(r::DataFrameRow) collect(pairs(r)) - import Base: show @deprecate show(io::IO, df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol, summary::Bool) show(io, df, allcols=allcols, rowlabel=rowlabel, summary=summary) @deprecate show(io::IO, df::AbstractDataFrame, allcols::Bool, rowlabel::Symbol) show(io, df, allcols=allcols, rowlabel=rowlabel) diff --git a/test/dataframerow.jl b/test/dataframerow.jl index 0ae2244a34..0ed8ebb448 100644 --- a/test/dataframerow.jl +++ b/test/dataframerow.jl @@ -89,7 +89,7 @@ module TestDataFrameRow r = DataFrameRow(df, 1) @test r[:] == r - # keys, values and iteration + # keys, values and iteration, size @test keys(r) == names(df) @test values(r) == (df[1, 1], df[1, 2], df[1, 3], df[1, 4]) @test collect(pairs(r)) == [:a=>df[1, 1], :b=>df[1, 2], :c=>df[1, 3], :d=>df[1, 4]] @@ -97,6 +97,12 @@ module TestDataFrameRow @test haskey(r, :a) @test !haskey(r, :zzz) + @test length(r) == 4 + @test ndims(r) == 1 + @test size(r) == (4,) + @test size(r, 1) == 4 + @test_throws BoundsError size(r, 2) + df = DataFrame(a=nothing, b=1) io = IOBuffer() show(io, DataFrameRow(df, 1)) @@ -116,4 +122,16 @@ module TestDataFrameRow c=["A", "B", "C", "A", "B", missing]) @test copy(DataFrameRow(df, 1)) == (a = 1, b = 2.0, c = "A") @test isequal(copy(DataFrameRow(df, 2)), (a = 2, b = missing, c = "B")) + + # iteration and collect + ref = ["a", "b", "c"] + df = DataFrame(permutedims(ref)) + dfr = df[1, :] + @test collect(dfr) == ref + for (v1, v2) in zip(ref, dfr) + @test v1 == v2 + end + for (i, v) in enumerate(dfr) + @test v == ref[i] + end end From 0a8769ee73d6cbf06aa43d89d1aed4e27c59f26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Fri, 14 Dec 2018 21:26:15 +0100 Subject: [PATCH 2/6] add eltype to DataFrameRow --- src/dataframerow/dataframerow.jl | 3 ++- test/dataframerow.jl | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 6c162d31ba..57a6306ec5 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -91,6 +91,8 @@ function Base.iterate(r::DataFrameRow, st) return (r[st], st + 1) end +Base.eltype(r::DataFrameRow) = reduce(typejoin, eltypes(parent(r))) + Base.convert(::Type{Array}, dfr::DataFrameRow) = [dfr[i] for j in 1:1, i in 1:length(dfr)] Base.convert(::Type{Vector}, dfr::DataFrameRow) = @@ -100,7 +102,6 @@ Base.convert(::Type{Vector{T}}, dfr::DataFrameRow) where T = Base.Vector(dfr::DataFrameRow) = convert(Vector, dfr) Base.Vector{T}(dfr::DataFrameRow) where T = convert(Vector{T}, dfr) - Base.keys(r::DataFrameRow) = names(parent(r)) Base.values(r::DataFrameRow) = ntuple(col -> parent(r)[col][row(r)], length(r)) diff --git a/test/dataframerow.jl b/test/dataframerow.jl index 0ed8ebb448..9afcc03ed6 100644 --- a/test/dataframerow.jl +++ b/test/dataframerow.jl @@ -128,10 +128,13 @@ module TestDataFrameRow df = DataFrame(permutedims(ref)) dfr = df[1, :] @test collect(dfr) == ref + @test eltype(collect(dfr)) === String for (v1, v2) in zip(ref, dfr) @test v1 == v2 end for (i, v) in enumerate(dfr) @test v == ref[i] end + dfr = DataFrame(a=1, b=true, c=1.0)[1,:] + @test eltype(collect(dfr)) === Real end From da3671d31f40037762486fd732e5d4038e5b7fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Fri, 14 Dec 2018 22:04:17 +0100 Subject: [PATCH 3/6] additional ndims method --- src/dataframerow/dataframerow.jl | 1 + test/dataframerow.jl | 1 + 2 files changed, 2 insertions(+) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 57a6306ec5..0a7c59cf34 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -81,6 +81,7 @@ Base.size(r::DataFrameRow) = (size(parent(r), 2),) Base.size(r::DataFrameRow, i) = size(r)[i] Base.length(r::DataFrameRow) = size(parent(r), 2) Base.ndims(r::DataFrameRow) = 1 +Base.ndims(::Type{DataFrameRow{T}}) where {T<:AbstractDataFrame} = 1 Base.lastindex(r::DataFrameRow) = size(parent(r), 2) diff --git a/test/dataframerow.jl b/test/dataframerow.jl index 9afcc03ed6..9e5c96841a 100644 --- a/test/dataframerow.jl +++ b/test/dataframerow.jl @@ -99,6 +99,7 @@ module TestDataFrameRow @test length(r) == 4 @test ndims(r) == 1 + @test ndims(typeof(r)) == 1 @test size(r) == (4,) @test size(r, 1) == 4 @test_throws BoundsError size(r, 2) From 75b73ac7e31eef5e802b584e38dd5e9c867b8774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sat, 15 Dec 2018 15:21:47 +0100 Subject: [PATCH 4/6] replace eltype by EltypeUnknown --- src/dataframerow/dataframerow.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 0a7c59cf34..70ac18d9b3 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -92,7 +92,7 @@ function Base.iterate(r::DataFrameRow, st) return (r[st], st + 1) end -Base.eltype(r::DataFrameRow) = reduce(typejoin, eltypes(parent(r))) +Base.IteratorEltype(::DataFrameRow) = Base.EltypeUnknown() Base.convert(::Type{Array}, dfr::DataFrameRow) = [dfr[i] for j in 1:1, i in 1:length(dfr)] From 0b1cf5edb427ede8893931b5f0b6e8b8e5537f35 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Sat, 15 Dec 2018 16:26:44 +0100 Subject: [PATCH 5/6] Update src/dataframerow/dataframerow.jl Co-Authored-By: bkamins --- src/dataframerow/dataframerow.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index 70ac18d9b3..68e0e611cf 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -81,7 +81,7 @@ Base.size(r::DataFrameRow) = (size(parent(r), 2),) Base.size(r::DataFrameRow, i) = size(r)[i] Base.length(r::DataFrameRow) = size(parent(r), 2) Base.ndims(r::DataFrameRow) = 1 -Base.ndims(::Type{DataFrameRow{T}}) where {T<:AbstractDataFrame} = 1 +Base.ndims(::Type{<:DataFrameRow}) = 1 Base.lastindex(r::DataFrameRow) = size(parent(r), 2) From a594aff805ae1dc9ece4b8c2d7fb67107f0f74f9 Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Mon, 17 Dec 2018 21:24:26 +0100 Subject: [PATCH 6/6] Add comment --- src/dataframerow/dataframerow.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/dataframerow/dataframerow.jl b/src/dataframerow/dataframerow.jl index bc34a64b39..af25799ff1 100644 --- a/src/dataframerow/dataframerow.jl +++ b/src/dataframerow/dataframerow.jl @@ -88,6 +88,8 @@ function Base.iterate(r::DataFrameRow, st) return (r[st], st + 1) end +# Computing the element type requires going over all columns, +# so better let collect() do it only if necessary (widening) Base.IteratorEltype(::DataFrameRow) = Base.EltypeUnknown() Base.convert(::Type{Array}, dfr::DataFrameRow) =