Skip to content

Commit

Permalink
Materialize DataFrame(Rows|Columns) as DataFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf authored and bkamins committed Dec 20, 2019
1 parent 4d323ca commit 9ebbf6f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 18 deletions.
18 changes: 2 additions & 16 deletions src/other/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,8 @@ Tables.rowaccess(::Type{<:Union{DataFrameRows,DataFrameColumns}}) = true
Tables.columns(itr::Union{DataFrameRows,DataFrameColumns}) = Tables.columns(parent(itr))
Tables.rows(itr::Union{DataFrameRows,DataFrameColumns}) = Tables.rows(parent(itr))
Tables.schema(itr::Union{DataFrameRows,DataFrameColumns}) = Tables.schema(parent(itr))
Tables.materializer(itr::DataFrameRows) =
eachrow prefer_singleton_callable(Tables.materializer(parent(itr)))
function Tables.materializer(itr::DataFrameColumns)
f = prefer_singleton_callable(Tables.materializer(parent(itr)))
if eltype(itr) <: Pair
return x -> eachcol(f(x), true)
else
return x -> eachcol(f(x), false)
end
end

# A hack to workaround the type-instability of `∘`:
prefer_singleton_callable(::Type{T}) where T = SingletonCallable{T}()
prefer_singleton_callable(f) = f
struct SingletonCallable{T} end
(::SingletonCallable{T})(x) where T = T(x)
Tables.materializer(itr::Union{DataFrameRows,DataFrameColumns}) =
Tables.materializer(parent(itr))

IteratorInterfaceExtensions.getiterator(df::AbstractDataFrame) = Tables.datavaluerows(df)
IteratorInterfaceExtensions.isiterable(x::AbstractDataFrame) = true
Expand Down
4 changes: 2 additions & 2 deletions test/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ Base.propertynames(d::DuplicateNamesColumnTable) = (:a, :a, :b)

@testset "basics $(nameof(typeof(table)))" for table in [
df,
view(df, :, :),
eachrow(df),
eachcol(df),
eachcol(df, true),
]
@test Tables.istable(table)
@test Tables.rowaccess(table)
@test Tables.columnaccess(table)
@test Tables.schema(table) === Tables.Schema((:a, :b), Tuple{Int64, Symbol})
@test Tables.schema(table) == Tables.schema(Tables.rows(table)) == Tables.schema(Tables.columns(table))
@test @inferred(Tables.materializer(table)(Tables.columns(table))) isa typeof(table)
@test @inferred(Tables.materializer(table)(Tables.columns(table))) isa DataFrame

row = first(Tables.rows(table))
@test propertynames(row) == (:a, :b)
Expand Down

0 comments on commit 9ebbf6f

Please sign in to comment.