Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ByRow subtype Function #2212

Merged
merged 5 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 7 additions & 11 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,28 +99,27 @@ 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
else
rawc = first(sel)
wanttable = false
end
if rawc isa AbstractVector{Int}
if rawc isa AbstractVector{Int}
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
c = rawc
elseif rawc isa Union{AbstractVector{Symbol}, AbstractVector{<:AbstractString}}
c = [idx[n] for n in rawc]
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