Skip to content

Commit

Permalink
Merge pull request #264 from pdeffebach/quotenode_lhs
Browse files Browse the repository at this point in the history
`:y = f(:x)` instead of `y = f(:x)`
  • Loading branch information
pdeffebach authored Jul 7, 2021
2 parents 77ccad7 + 227a261 commit 3250d35
Show file tree
Hide file tree
Showing 15 changed files with 611 additions and 562 deletions.
46 changes: 22 additions & 24 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ df = DataFrame(a = [1, 2], b = [3, 4]);
transform(df, [:a, :b] => ((a, b) -> a .* b .+ first(a) .- sum(b)) => :c);

# With DataFramesMeta
@transform(df, c = :a .* :b .+ first(:a) .- sum(:b))
@transform(df, :c = :a .* :b .+ first(:a) .- sum(:b))
```

To reference columns inside DataFramesMeta macros, use `Symbol`s. For example, use `:x`
Expand Down Expand Up @@ -66,11 +66,11 @@ data frame.
df = DataFrame(x = [1, 1, 2, 2], y = [1, 2, 101, 102]);
gd = groupby(df, :x);
@select(df, :x, :y)
@select(df, x2 = 2 * :x, :y)
@select(gd, x2 = 2 .* :y .* first(:y))
@select(df, :x2 = 2 * :x, :y)
@select(gd, :x2 = 2 .* :y .* first(:y))
@select!(df, :x, :y)
@select!(df, x = 2 * :x, :y)
@select!(gd, y = 2 .* :y .* first(:y))
@select!(df, :x = 2 * :x, :y)
@select!(gd, :y = 2 .* :y .* first(:y))
```

## `@transform` and `@transform!`
Expand All @@ -89,11 +89,11 @@ data frame.
df = DataFrame(x = [1, 1, 2, 2], y = [1, 2, 101, 102]);
gd = groupby(df, :x);
@transform(df, :x, :y)
@transform(df, x2 = 2 * :x, :y)
@transform(gd, x2 = 2 .* :y .* first(:y))
@transform(df, :x2 = 2 * :x, :y)
@transform(gd, :x2 = 2 .* :y .* first(:y))
@transform!(df, :x, :y)
@transform!(df, x = 2 * :x, :y)
@transform!(gd, y = 2 .* :y .* first(:y))
@transform!(df, :x = 2 * :x, :y)
@transform!(gd, :y = 2 .* :y .* first(:y))
```

## `@subset` and `@subset!`
Expand Down Expand Up @@ -124,8 +124,8 @@ Examples:
```julia
df = DataFrame(x = [1, 1, 2, 2], y = [1, 2, 101, 102]);
gd = groupby(df, :x);
@combine(gd, x2 = sum(:y))
@combine(gd, x2 = :y .- sum(:y))
@combine(gd, :x2 = sum(:y))
@combine(gd, :x2 = :y .- sum(:y))
@combine(gd, (n1 = sum(:y), n2 = first(:y)))
```

Expand Down Expand Up @@ -255,14 +255,14 @@ end

`@eachrow` also supports special syntax for allocating new columns to make
`@eachrow` more useful for data transformations. The syntax `@newcol
x::Vector{Int}` allocates a new column `:x` with an `Vector` container with eltype
:x::Vector{Int}` allocates a new column `:x` with an `Vector` container with eltype
`Int`. Here is an example where two new columns are added:

```julia
df = DataFrame(A = 1:3, B = [2, 1, 2])
df2 = @eachrow df begin
@newcol colX::Vector{Float64}
@newcol colY::Vector{Union{Int,Missing}}
@newcol :colX::Vector{Float64}
@newcol :colY::Vector{Union{Int,Missing}}
:colX = :B == 2 ? pi * :A : :B
if :A > 1
:colY = :A * :B
Expand All @@ -289,7 +289,7 @@ Thought of as a macro `@byrow` accepts a single argument and
creates an anonymous function wrapped in `ByRow`. For example,

```julia
@transform(df, @byrow y = :x == 1 ? true : false)
@transform(df, @byrow :y = :x == 1 ? true : false)
```

is equivalent to
Expand Down Expand Up @@ -327,7 +327,7 @@ julia> @where df @byrow begin
however, like with `ByRow` in DataFrames.jl, when `@byrow` is
used, functions do not take into account the grouping, so for
example the result of `@transform(df, @byrow y = f(:x))` and
`@transform(groupby(df, :g), @byrow y = f(:x))` is the same.
`@transform(groupby(df, :g), @byrow :y = f(:x))` is the same.

## Working with column names programmatically with `cols`

Expand Down Expand Up @@ -469,11 +469,11 @@ df = DataFrame(a = repeat(1:5, outer = 20),
x = repeat(1:20, inner = 5))

x_thread = @chain df begin
@transform(y = 10 * :x)
@transform(:y = 10 * :x)
@where(:a .> 2)
@by(:b, meanX = mean(:x), meanY = mean(:y))
@by(:b, :meanX = mean(:x), :meanY = mean(:y))
@orderby(:meanX)
@select(:meanX, :meanY, var = :b)
@select(:meanX, :meanY, :var = :b)
end
```

Expand All @@ -487,7 +487,7 @@ expression.
# Get the sum of all columns after
# a few transformations
@chain df begin
@transform(y = 10 .* :x)
@transform(:y = 10 .* :x)
@where(:a .> 2)
@select(:a, :y, :x)
reduce(+, eachcol(_))
Expand All @@ -499,14 +499,12 @@ in the middle of a `@chain` block.

```julia
@chain df begin
@transform y = 10 .* :x
@transform :y = 10 .* :x
@aside y_mean = mean(_.y) # From Chain.jl, not DataFramesMeta.jl
@select y_standardize = :y .- y_mean
@select :y_standardize = :y .- y_mean
end
```



```@contents
Pages = ["api/api.md"]
Depth = 3
Expand Down
12 changes: 6 additions & 6 deletions src/eachrow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ julia> @eachrow df begin
3 │ 0 2
julia> df2 = @eachrow df begin
@newcol colX::Vector{Float64}
@newcol :colX::Vector{Float64}
:colX = :B == 2 ? pi * :A : :B
end
3×3 DataFrame
Expand All @@ -157,7 +157,7 @@ julia> df2 = @eachrow df begin
julia> varA = :A; varB = :B;
julia> df2 = @eachrow df begin
@newcol colX::Vector{Float64}
@newcol :colX::Vector{Float64}
:colX = cols(varB) == 2 ? pi * cols(varA) : cols(varB)
end
3×3 DataFrame
Expand All @@ -181,7 +181,7 @@ julia> x
3
julia> @eachrow df begin
@newcol m::Vector{Float64}
@newcol :m::Vector{Float64}
:m = mean(_DF[:, row])
end
3×3 DataFrame
Expand Down Expand Up @@ -290,7 +290,7 @@ julia> df2
julia> df2 = copy(df);
julia> @eachrow! df2 begin
@newcol colX::Vector{Float64}
@newcol :colX::Vector{Float64}
:colX = :B == 2 ? pi * :A : :B
end
3×3 DataFrame
Expand All @@ -306,7 +306,7 @@ julia> varA = :A; varB = :B;
julia> df2 = copy(df);
julia> @eachrow! df2 begin
@newcol colX::Vector{Float64}
@newcol :colX::Vector{Float64}
:colX = cols(varB) == 2 ? pi * cols(varA) : cols(varB)
end
3×3 DataFrame
Expand All @@ -330,7 +330,7 @@ julia> x
3
julia> @eachrow! df begin
@newcol m::Vector{Float64}
@newcol :m::Vector{Float64}
:m = mean(_DF[:, row])
end
3×3 DataFrame
Expand Down
11 changes: 7 additions & 4 deletions src/linqmacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export @linq, linq
"""
@linq df ...
!!! note
`@linq` is deprecated. Use `@chain` instead. See `? @chain` for details.
General macro that creates a mini DSL for chaining and macro calls.
### Details
Expand All @@ -34,11 +37,11 @@ julia> df = DataFrame(
b = repeat(2:-1:1, outer = 4),
x = 1:8);
julia> x1 = @linq transform(where(df, :a .> 2, :b .!= "c"), y = 10 .* :x);
julia> x1 = @linq transform(where(df, :a .> 2, :b .!= "c"), :y = 10 .* :x);
julia> x1 = @linq by(x1, :b, meanX = mean(:x), meanY = mean(:y));
julia> x1 = @linq by(x1, :b, :meanX = mean(:x), :meanY = mean(:y));
julia> @linq select(orderby(x1, :b, -:meanX), var = :b, :meanX, :meanY)
julia> @linq select(orderby(x1, :b, -:meanX), :var = :b, :meanX, :meanY)
2×3 DataFrame
│ Row │ var │ meanX │ meanY │
│ │ Int64 │ Float64 │ Float64 │
Expand All @@ -49,7 +52,7 @@ julia> @linq select(orderby(x1, :b, -:meanX), var = :b, :meanX, :meanY)
julia> @linq df |>
transform(y = 10 .* :x) |>
where(:a .> 2) |>
by(:b, meanX = mean(:x), meanY = mean(:y)) |>
by(:b, :meanX = mean(:x), :meanY = mean(:y)) |>
orderby(:meanX) |>
select(:meanX, :meanY, var = :b)
2×3 DataFrame
Expand Down
Loading

0 comments on commit 3250d35

Please sign in to comment.