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

Implementing row function syntax to Map transform #291

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 8 additions & 1 deletion src/transforms/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ Map() = throw(ArgumentError("cannot create Map transform without arguments"))
const TargetName = Union{Symbol,AbstractString}
const PairWithTarget = Pair{<:Any,<:Pair{<:Function,<:TargetName}}
const PairWithoutTarget = Pair{<:Any,<:Function}
const MapPair = Union{PairWithTarget,PairWithoutTarget}
const PairFunctionTarget = Pair{<:Function,<:TargetName}
const MapPair = Union{PairWithTarget,PairWithoutTarget,PairFunctionTarget,Function}

# utility functions
_extract(p::PairWithTarget) = selector(first(p)), first(last(p)), Symbol(last(last(p)))
_extract(p::PairWithoutTarget) = selector(first(p)), last(p), nothing
_extract(p::PairFunctionTarget) = AllSelector(), first(p), Symbol(last(p))
_extract(p::Function) = AllSelector(), p, nothing

function Map(pairs::MapPair...)
tuples = map(_extract, pairs)
Expand Down Expand Up @@ -93,6 +96,10 @@ function applyfeat(transform::Map, feat, prep)
mapped = map(selectors, funs, targets) do selector, fun, target
snames = selector(names)
newname = isnothing(target) ? _makename(snames, fun) : target
if selector isa AllSelector
newcolumn = map(fun, Tables.rows(cols))
return newname => newcolumn
end
scolumns = (Tables.getcolumn(cols, nm) for nm in snames)
newcolumn = map(fun, scolumns...)
newname => newcolumn
Expand Down
29 changes: 29 additions & 0 deletions test/transforms/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,33 @@

# error: cannot create Map transform without arguments
@test_throws ArgumentError Map()

# row functions
## no target
frow = row -> row.a + row.b - row.c
fname = replace(string(frow), "#" => "f")
colname = Symbol(fname, :_a,:_b,:_c,:_d)
T = Map(frow)
n, c = apply(T, t)
@test Tables.schema(n).names == (:a, :b, :c, :d, colname)
@test Tables.getcolumn(n, colname) == frow.(t)

## no target with extra functions
T = Map(frow, :a => (a->a) => :A)
n, c = apply(T, t)
Tables.schema(n).names == (:a, :b, :c, :d, colname,:A)
Tables.getcolumn(n, colname) == frow.(t)

## target column
T = Map((row -> sum(row)) => :summation)
n, c = apply(T, t)
@test Tables.schema(n).names == (:a, :b, :c, :d, :summation)
@test map(row->sum(row),t) == n.summation

## target column with extra function
T = Map((row -> row.a + row.b) => :a_plus_b, :a => (a -> a) => :A)
n, c = apply(T, t)
@test Tables.schema(n).names == (:a, :b, :c, :d, :a_plus_b,:A)
@test map(row->row.a + row.b,t) == n.a_plus_b

end
Loading