Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -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 0.3.0
DataValues 0.4.4
7 changes: 4 additions & 3 deletions src/Pandas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 8 additions & 5 deletions src/tabletraits.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions test/test_tabletraits.jl
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)
Expand Down