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

add in-place macros @transform! and @select! #216

Merged
merged 8 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 6 additions & 4 deletions src/DataFramesMeta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,16 +621,18 @@ julia> using DataFramesMeta

julia> df = DataFrame(A = 1:3, B = [2, 1, 2]);

julia> @transform!(df, a = 2 * :A, x = :A .+ :B);
julia> df2 = @transform!(df, a = 2 * :A, x = :A .+ :B);

FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved
FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved
julia> df
julia> (df === df2) && df2
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved
3×4 DataFrame
│ Row │ A │ B │ a │ x │
│ │ Int64 │ Int64 │ Int64 │ Int64 │
├─────┼───────┼───────┼───────┼───────┤
│ 1 │ 1 │ 2 │ 2 │ 3 │
│ 2 │ 2 │ 1 │ 4 │ 3 │
│ 3 │ 3 │ 2 │ 6 │ 5 │

julia> @transform!
FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved
```
"""
macro transform!(x, args...)
Expand Down Expand Up @@ -978,9 +980,9 @@ julia> df

julia> df = DataFrame(a = repeat(1:4, outer = 2), b = repeat(2:-1:1, outer = 4), c = 1:8);

julia> @select!(df, :c, x = :b + :c);
julia> df2 = @select!(df, :c, x = :b + :c);

julia> df
julia> (df === df2) && df2
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved

FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved
8×2 DataFrame
FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved
│ Row │ c │ x │
Expand Down
159 changes: 157 additions & 2 deletions test/dataframes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,81 @@ const ≅ = isequal
@test newdf ≅ df
end


@testset "@transform!" begin
df = DataFrame(
g = [1, 1, 1, 2, 2],
i = 1:5,
t = ["a", "b", "c", "c", "e"],
y = [:v, :w, :x, :y, :z],
c = [:g, :quote, :body, :transform, missing]
)

m = [100, 200, 300, 400, 500]

gq = :g
iq = :i
tq = :t
yq = :y
cq = :c

gr = "g"
ir = "i"
tr = "t"
yr = "y"
cr = "c"

n_str = "new_column"
n_sym = :new_column
n_space = "new column"

@test @transform!(df, n = :i).n == df.i
@test @transform!(df, n = :i .+ :g).n == df.i .+ df.g
@test @transform!(df, n = :t .* string.(:y)).n == df.t .* string.(df.y)
@test @transform!(df, n = Symbol.(:y, ^(:t))).n == Symbol.(df.y, :t)
@test @transform!(df, n = Symbol.(:y, ^(:body))).n == Symbol.(df.y, :body)
@test @transform!(df, body = :i).body == df.i
@test @transform!(df, transform = :i).transform == df.i

@test @transform!(df, n = cols(iq)).n == df.i
@test @transform!(df, n = cols(iq) .+ cols(gq)).n == df.i .+ df.g
@test @transform!(df, n = cols(tq) .* string.(cols(yq))).n == df.t .* string.(df.y)
@test @transform!(df, n = Symbol.(cols(yq), ^(:t))).n == Symbol.(df.y, :t)
@test @transform!(df, n = Symbol.(cols(yq), ^(:body))).n == Symbol.(df.y, :body)
@test @transform!(df, body = cols(iq)).body == df.i
@test @transform!(df, transform = cols(iq)).transform == df.i

@test @transform!(df, n = cols(ir)).n == df.i
@test @transform!(df, n = cols(ir) .+ cols(gr)).n == df.i .+ df.g
@test @transform!(df, n = cols(tr) .* string.(cols(yr))).n == df.t .* string.(df.y)
@test @transform!(df, n = Symbol.(cols(yr), ^(:t))).n == Symbol.(df.y, :t)
@test @transform!(df, n = Symbol.(cols(yr), ^(:body))).n == Symbol.(df.y, :body)
@test @transform!(df, body = cols(ir)).body == df.i
@test @transform!(df, transform = cols(ir)).transform == df.i
@test @transform!(df, n = cols("g") + cols(:i)).n == df.g + df.i
@test @transform!(df, n = cols(1) + cols(2)).n == df.g + df.i

@test @transform!(df, cols("new_column") = :i).new_column == df.i
@test @transform!(df, cols(n_str) = :i).new_column == df.i
@test @transform!(df, cols(n_sym) = :i).new_column == df.i
@test @transform!(df, cols(n_space) = :i)."new column" == df.i
@test @transform!(df, cols("new" * "_" * "column") = :i).new_column == df.i

@test @transform!(df, n = 1).n == fill(1, nrow(df))
@test @transform!(df, n = :i .* :g).n == [1, 2, 3, 8, 10]

# non-copying
@test @transform!(df, n = :i).g === df.g
@test @transform!(df, n = :i).n === df.i
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
# mutating
df2 = copy(df)
@transform!(df, n = :i)
df == @transform(df2, n = :i)
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved



end
FuZhiyu marked this conversation as resolved.
Show resolved Hide resolved

# Defined outside of `@testset` due to use of `@eval`
df = DataFrame(
g = [1, 1, 1, 2, 2],
Expand Down Expand Up @@ -198,8 +273,88 @@ end

@test @transform(df, n = :i .* :g).n == [1, 2, 3, 8, 10]

@select!(df, :i, :g, n = :i .+ :g)
@test df == df2[!, [:i, :g, :n]]
end

@testset "@select!" begin
# Defined outside of `@testset` due to use of `@eval`
df = DataFrame(
g = [1, 1, 1, 2, 2],
i = 1:5,
t = ["a", "b", "c", "c", "e"],
y = [:v, :w, :x, :y, :z],
c = [:g, :quote, :body, :transform, missing]
)

m = [100, 200, 300, 400, 500]

gq = :g
iq = :i
tq = :t
yq = :y
cq = :c

gr = "g"
ir = "i"
tr = "t"
yr = "y"
cr = "c"

n_str = "new_column"
n_sym = :new_column
n_space = "new column"

df2 = copy(df)
df2.n = df2.i .+ df2.g

@test @select!(copy(df), :i, :g, n = :i .+ :g) == df2[!, [:i, :g, :n]]
@test @select!(copy(df), :i, :g) == df2[!, [:i, :g]]
@test @select!(copy(df), :i) == df2[!, [:i]]

@test @select!(copy(df), n = :i .+ :g).n == df.i .+ df.g
@test @select!(copy(df), n = :i).n == df2.i
@test @select!(copy(df), n = :t .* string.(:y)).n == df.t .* string.(df.y)
@test @select!(copy(df), n = Symbol.(:y, ^(:t))).n == Symbol.(df.y, :t)
@test @select!(copy(df), n = Symbol.(:y, ^(:body))).n == Symbol.(df.y, :body)
@test @select!(copy(df), body = :i).body == df.i
@test @select!(copy(df), transform = :i).transform == df.i

@test @select!(copy(df), n = cols(iq)).n == df.i
@test @select!(copy(df), n = cols(iq) .+ cols(gq)).n == df.i .+ df.g
@test @select!(copy(df), n = cols(tq) .* string.(cols(yq))).n == df.t .* string.(df.y)
@test @select!(copy(df), n = Symbol.(cols(yq), ^(:t))).n == Symbol.(df.y, :t)
@test @select!(copy(df), n = Symbol.(cols(yq), ^(:body))).n == Symbol.(df.y, :body)
@test @select!(copy(df), body = cols(iq)).body == df.i
@test @select!(copy(df), transform = cols(iq)).transform == df.i

@test @select!(copy(df), n = cols(ir)).n == df.i
@test @select!(copy(df), n = cols(ir) .+ cols(gr)).n == df.i .+ df.g
@test @select!(copy(df), n = cols(tr) .* string.(cols(yr))).n == df.t .* string.(df.y)
@test @select!(copy(df), n = Symbol.(cols(yr), ^(:t))).n == Symbol.(df.y, :t)
@test @select!(copy(df), n = Symbol.(cols(yr), ^(:body))).n == Symbol.(df.y, :body)
@test @select!(copy(df), body = cols(ir)).body == df.i
@test @select!(copy(df), transform = cols(ir)).transform == df.i
@test @select!(copy(df), n = cols("g") + cols(:i)).n == df.g + df.i
@test @select!(copy(df), n = cols(1) + cols(2)).n == df.g + df.i


@test @select!(copy(df), n = 1).n == fill(1, nrow(df))

@test @select!(copy(df), cols("new_column") = :i).new_column == df.i
@test @select!(copy(df), cols(n_str) = :i).new_column == df.i
@test @select!(copy(df), cols(n_sym) = :i).new_column == df.i
@test @select!(copy(df), cols(n_space) = :i)."new column" == df.i
@test @select!(copy(df), cols("new" * "_" * "column") = :i).new_column == df.i

# non-copying
newcol = [1:5;]
pdeffebach marked this conversation as resolved.
Show resolved Hide resolved
df2 = copy(df)
df2.newcol = newcol
@test @select!(df2, :newcol).newcol === newcol

# mutating
df2 = @select(df, :i)
@select!(df, :i)
@test df == df2
end

# Defined outside of `@testset` due to use of `@eval`
Expand Down