Skip to content

Commit

Permalink
Automatically call eachrow for DataFrames
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Dec 26, 2019
1 parent 2289359 commit 2c13b21
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/Transducers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function __init__()
@require OnlineStatsBase="925886fa-5bf2-5e8e-b522-a9147a512338" begin
include("interop/onlinestats.jl")
end
@require DataFrames="a93c6f00-e57d-5684-b7b6-d8193f3e46c0" begin
include("interop/dataframes.jl")
end
end

end # module
1 change: 1 addition & 0 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ identityof(::typeof(right), ::Any) = nothing

abstract type Reducible end
abstract type Foldable <: Reducible end
asfoldable(x) = x

abstract type AbstractInitializer end

Expand Down
3 changes: 3 additions & 0 deletions src/interop/dataframes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
asfoldable(df::DataFrames.AbstractDataFrame) = DataFrames.eachrow(df)
# We can't use `Compat.eachrow` here. See:
# https://github.com/JuliaData/DataFrames.jl/pull/2067
13 changes: 10 additions & 3 deletions src/processes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ _unreduced__foldl__(rf, step, coll) = unreduced(__foldl__(rf, step, coll))
# `darkritual` below to work.
rf = maybe_usesimd(rf0, simd)
state = _start_init(rf, init)
result = __foldl__(rf, state, coll)
foldable = asfoldable(coll)
result = __foldl__(rf, state, foldable)
if unreduced(result) isa DefaultInit
throw(EmptyResultError(rf0))
# Should I check if `init` is a `MissingInit`?
Expand All @@ -366,7 +367,7 @@ _unreduced__foldl__(rf, step, coll) = unreduced(__foldl__(rf, step, coll))
# not change the return type.
realtype = _nonidtype(Core.Compiler.return_type(
_unreduced__foldl__,
typeof((rf0, state, coll)),
typeof((rf0, state, foldable)),
))
if realtype isa Type
realvalue = convert(realtype, ur_result)
Expand Down Expand Up @@ -633,7 +634,13 @@ julia> @assert copy(Map(x -> (a=x, b=x^2)), Table, 1:1) == Table(a=[1], b=[1])
julia> using StructArrays
julia> @assert copy(Map(x -> (a=x, b=x^2)), StructVector, 1:1) == StructVector(a=[1], b=[1])
```
julia> using DataFrames
julia> @assert copy(
Map(x -> (A = x.a + 1, B = x.b + 1)),
DataFrame(a = [1], b = [2]),
) == DataFrame(A = [2], B = [3])
"""
Base.copy(xf::Transducer, ::Type{T}, foldable) where {T} = append!!(xf, Empty(T), foldable)
Base.copy(xf::Transducer, foldable) = copy(xf, _materializer(foldable), foldable)
Expand Down
9 changes: 7 additions & 2 deletions test/test_copy.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestCopy

include("preamble.jl")
using DataFrames: DataFrame
using DataFrames: DataFrame, eachrow
using StructArrays: StructVector
using TypedTables: Table

Expand All @@ -14,10 +14,15 @@ end

@testset "$copy" for copy in [copy, tcopy, dcopy]
@testset "$copy(_, ::$(prettytypeof(src)))" for src in Any[
# DataFrame(a=[1], b=[2]),
DataFrame(a=[1], b=[2]),
StructVector(a=[1:4;], b=[5:8;]),
Table(a=[1:4;], b=[5:8;]),
]
if copy in (tcopy, dcopy) && src isa DataFrame
@test_broken copy(Map(identity), src) ==ₜ src
@test_broken copy(Map(identity), src; basesize=1) ==ₜ src
continue
end
@test copy(Map(identity), src) ==ₜ src
if copy in (tcopy, dcopy)
@test copy(Map(identity), src; basesize=1) ==ₜ src
Expand Down

0 comments on commit 2c13b21

Please sign in to comment.