What expressions do the macros create?
With the following @prettyexpand
macro, we can have a look at exactly what output expressions the different macros and flags produce. All animal name variables are temporary variables that are macro-generated.
using JuliaFormatter: format_text
using MacroTools: prettify
using Markdown
macro prettyexpand(exp)
s = format_text(string(
prettify(macroexpand(@__MODULE__, exp))
), margin = 80)
Markdown.parse("""
```julia
$s
```
""")
end
ByRow by default
A simple select call with two column names and one output name.
using DataFrameMacros
@prettyexpand @select(df, :z = :x + :y)
select(df, vcat.("x", "y") .=> (ByRow(+) .=> "z");)
@c
The @c
flag macro removes the ByRow
construct.
@prettyexpand @select(df, :z = @c :x .+ :y)
select(df, vcat.("x", "y") .=> ((.+) .=> "z");)
Integer columns
Integers have to be resolved to strings before being used as column identifiers.
@prettyexpand @select(df, :z = $1 + $2)
select(
df,
vcat.(
DataFrameMacros.stringargs(1, df),
DataFrameMacros.stringargs(2, df),
) .=> (ByRow(+) .=> "z");,
)
@m
The @m
flag introduces a passmissing
wrapper.
@prettyexpand @select(df, :z = @m :x + :y)
select(df, vcat.("x", "y") .=> (ByRow(passmissing(+)) .=> "z");)
@t
The @t
flag sets the output to AsTable
and creates a NamedTuple
of all symbol assignments.
@prettyexpand @transform(df, @t :first_name, :last_name = split(:full_name))
transform(
df,
vcat.("full_name") .=> (
ByRow((
(hummingbird,) -> begin
(badger, hippopotamus) = split(hummingbird)
(first_name = badger, last_name = hippopotamus)
end
)) .=> DataFrames.AsTable
);,
)
begin end
block
@prettyexpand @select df begin
:z = :x + :y
:q = sqrt(:y / :x)
end
select(
df,
vcat.("x", "y") .=> (ByRow(+) .=> "z"),
vcat.("y", "x") .=>
(ByRow(((hummingbird, badger) -> sqrt(hummingbird / badger))) .=> "q");,
)