From 93b80fd604091596c392780116e9e0807ad839cb Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Sat, 1 Sep 2018 22:25:34 -0700 Subject: [PATCH 1/2] Update TableTraits.jl integration --- REQUIRE | 7 ++++--- src/Pandas.jl | 7 ++++--- src/tabletraits.jl | 13 ++++++++----- test/runtests.jl | 4 +--- test/test_tabletraits.jl | 23 ++++++++++++----------- 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/REQUIRE b/REQUIRE index 65f33f4..f8c6b9e 100644 --- a/REQUIRE +++ b/REQUIRE @@ -2,6 +2,7 @@ julia 1.0 PyCall 1.7.2 Lazy 0.11.4 Compat 0.17 -TableTraits 0.0.1 -TableTraitsUtils 0.0.1 -DataValues 0.0.1 +IteratorInterfaceExtensions 0.1.1 +TableTraits 0.3.1 +TableTraitsUtils +DataValues 0.4.4 diff --git a/src/Pandas.jl b/src/Pandas.jl index 7cc170a..a01c3f3 100644 --- a/src/Pandas.jl +++ b/src/Pandas.jl @@ -420,10 +420,11 @@ end include("tabletraits.jl") function DataFrame(obj) - if TableTraits.isiterabletable(obj) - _construct_pandas_from_iterabletable(obj) + y = _construct_pandas_from_iterabletable(obj) + if y===nothing + return invoke(DataFrame, Tuple{Vararg{Any}}, obj) else - invoke(DataFrame, Tuple{Vararg{Any}}, obj) + return y end end diff --git a/src/tabletraits.jl b/src/tabletraits.jl index 94da078..99ef0d3 100644 --- a/src/tabletraits.jl +++ b/src/tabletraits.jl @@ -1,7 +1,8 @@ +using IteratorInterfaceExtensions using TableTraitsUtils import DataValues -TableTraits.isiterable(x::DataFrame) = true +IteratorInterfaceExtensions.isiterable(x::DataFrame) = true TableTraits.isiterabletable(x::DataFrame) = true function TableTraits.getiterator(df::DataFrame) @@ -13,16 +14,18 @@ function TableTraits.getiterator(df::DataFrame) end function _construct_pandas_from_iterabletable(source) - columns, column_names = create_columns_from_iterabletable(source) - cols = Dict(i[1]=>i[2] for i in zip(column_names, columns)) + y = create_columns_from_iterabletable(source, errorhandling=:returnvalue) + y===nothing && return nothing + columns, column_names = y[1], y[2] + cols = Dict{Symbol,Any}(i[1]=>i[2] for i in zip(column_names, columns)) - for (k,v) in cols + for (k,v) in pairs(cols) if eltype(v)<:DataValues.DataValue T = eltype(eltype(v)) if T<:AbstractFloat cols[k] = T[get(i, NaN) for i in v] elseif T<:Integer - cols[k] = Float64[isnull(i) ? NaN : Float64(get(i)) for i in v] + cols[k] = Float64[DataValues.isna(i) ? NaN : Float64(get(i)) for i in v] else throw(ArgumentError("Can't create a Pandas.DataFrame from a source that has missing data.")) end diff --git a/test/runtests.jl b/test/runtests.jl index d9a7f07..c7eebc1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -12,9 +12,7 @@ df = read_csv(joinpath(dirname(@__FILE__), "test.csv")) typeof(df) @test isa(df, Pandas.DataFrame) -# Disabling this test until the 'queryverse' is fully -# 1.0-compatible. -# include("test_tabletraits.jl") +include("test_tabletraits.jl") @test !isempty(df) empty!(df) diff --git a/test/test_tabletraits.jl b/test/test_tabletraits.jl index e7bcb8a..6c62226 100644 --- a/test/test_tabletraits.jl +++ b/test/test_tabletraits.jl @@ -1,11 +1,12 @@ using Pandas -using NamedTuples +using IteratorInterfaceExtensions +using TableTraits using DataValues -using Base.Test +using Test @testset "TableTraits" begin -table_array = [@NT(a=1, b="John", c=3.2), @NT(a=2, b="Sally", c=5.8)] +table_array = [(a=1, b="John", c=3.2), (a=2, b="Sally", c=5.8)] df = DataFrame(table_array) @@ -16,26 +17,26 @@ df = DataFrame(table_array) @test TableTraits.isiterabletable(df) == true -it = TableTraits.getiterator(df) +it = IteratorInterfaceExtensions.getiterator(df) -@test eltype(it) == @NT(a::Int, b::String, c::Float64) +@test eltype(it) == NamedTuple{(:a,:b,:c),Tuple{Int,String,Float64}} it_collected = collect(it) -@test eltype(it_collected) == @NT(a::Int, b::String, c::Float64) +@test eltype(it_collected) == NamedTuple{(:a,:b,:c),Tuple{Int,String,Float64}} @test length(it_collected) == 2 -@test it_collected[1] == @NT(a=1, b="John", c=3.2) -@test it_collected[2] == @NT(a=2, b="Sally", c=5.8) +@test it_collected[1] == (a=1, b="John", c=3.2) +@test it_collected[2] == (a=2, b="Sally", c=5.8) -table_array2 = [@NT(a=1, b=DataValue("John"), c=3.2), @NT(a=2, b=DataValue("Sally"), c=5.8)] +table_array2 = [(a=1, b=DataValue("John"), c=3.2), (a=2, b=DataValue("Sally"), c=5.8)] @test_throws ArgumentError DataFrame(table_array2) -table_array3 = [@NT(a=DataValue{Int}(), b="John", c=DataValue(3.2)), @NT(a=DataValue(2), b="Sally", c=DataValue{Float64}())] +table_array3 = [(a=DataValue{Int}(), b="John", c=DataValue(3.2)), (a=DataValue(2), b="Sally", c=DataValue{Float64}())] df3 = DataFrame(table_array3) -it3_collected = collect(TableTraits.getiterator(df3)) +it3_collected = collect(IteratorInterfaceExtensions.getiterator(df3)) @test length(it3_collected) == 2 @test isnan(it3_collected[1].a) From 552cfb59954fcd6940a85ae7de784ee3a29b5850 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Sun, 2 Sep 2018 16:52:30 -0700 Subject: [PATCH 2/2] Update REQUIRE file --- REQUIRE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REQUIRE b/REQUIRE index f8c6b9e..0b7cac5 100644 --- a/REQUIRE +++ b/REQUIRE @@ -4,5 +4,5 @@ Lazy 0.11.4 Compat 0.17 IteratorInterfaceExtensions 0.1.1 TableTraits 0.3.1 -TableTraitsUtils +TableTraitsUtils 0.3.0 DataValues 0.4.4