Skip to content

Commit

Permalink
Make ByRow subtype Function
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox authored Apr 28, 2020
1 parent 3dd7ff0 commit 05a9c43
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/abstractdataframe/selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Note that `ByRow` always collects values returned by `fun` in a vector. Therefor
to allow for future extensions, returning `NamedTuple` or `DataFrameRow`
from `fun` is currently disallowed.
"""
struct ByRow{T}
struct ByRow{T} <: Function
fun::T
end

Expand All @@ -29,9 +29,6 @@ _by_row_helper(x::Union{NamedTuple, DataFrameRow}) =
throw(ArgumentError("return value of type $(typeof(x)) " *
"is currently not allowed with ByRow."))


Base.broadcastable(x::ByRow) = Ref(x)

(f::ByRow)(cols::AbstractVector...) = _by_row_helper.(f.fun.(cols...))
(f::ByRow)(table::NamedTuple) =
_by_row_helper.(f.fun.(Tables.namedtupleiterator(table)))
Expand Down Expand Up @@ -71,7 +68,7 @@ normalize_selection(idx::AbstractIndex, sel::Pair{<:ColumnIndex, <:AbstractStrin
normalize_selection(idx, first(sel) => Symbol(last(sel)))

function normalize_selection(idx::AbstractIndex,
sel::Pair{<:Any,<:Pair{<:Union{Base.Callable, ByRow}, Symbol}})
sel::Pair{<:Any,<:Pair{<:Base.Callable, Symbol}})
if first(sel) isa AsTable
rawc = first(sel).cols
wanttable = true
Expand Down Expand Up @@ -102,20 +99,19 @@ function normalize_selection(idx::AbstractIndex,
end

normalize_selection(idx::AbstractIndex,
sel::Pair{<:Any,<:Pair{<:Union{Base.Callable, ByRow},
<:AbstractString}}) =
sel::Pair{<:Any,<:Pair{<:Base.Callable,<:AbstractString}}) =
normalize_selection(idx, first(sel) => first(last(sel)) => Symbol(last(last(sel))))

function normalize_selection(idx::AbstractIndex,
sel::Pair{<:ColumnIndex,<:Union{Base.Callable, ByRow}})
sel::Pair{<:ColumnIndex,<:Base.Callable})
c = idx[first(sel)]
fun = last(sel)
newcol = Symbol(_names(idx)[c], "_", funname(fun))
return c => fun => newcol
end

function normalize_selection(idx::AbstractIndex,
sel::Pair{<:Any, <:Union{Base.Callable,ByRow}})
sel::Pair{<:Any, <:Base.Callable})
if first(sel) isa AsTable
rawc = first(sel).cols
wanttable = true
Expand Down Expand Up @@ -154,7 +150,7 @@ function normalize_selection(idx::AbstractIndex,
end

function select_transform!(nc::Pair{<:Union{Int, AbstractVector{Int}, AsTable},
<:Pair{<:Union{Base.Callable, ByRow}, Symbol}},
<:Pair{<:Base.Callable, Symbol}},
df::AbstractDataFrame, newdf::DataFrame,
transformed_cols::Dict{Symbol, Any}, copycols::Bool,
allow_resizing_newdf::Ref{Bool})
Expand Down
2 changes: 1 addition & 1 deletion src/groupeddataframe/splitapplycombine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ function _combine(f::AbstractVector{<:Pair},
gd::GroupedDataFrame, nms::AbstractVector{Symbol})
# here f should be normalized and in a form of source_cols => fun
@assert all(x -> first(x) isa Union{Int, AbstractVector{Int}, AsTable}, f)
@assert all(x -> last(x) isa Union{Base.Callable, ByRow}, f)
@assert all(x -> last(x) isa Base.Callable, f)
idx_agg = nothing
if any(isagg, f)
# Compute indices of representative rows only once for all AbstractAggregates
Expand Down

0 comments on commit 05a9c43

Please sign in to comment.