-
Notifications
You must be signed in to change notification settings - Fork 55
/
linqmacro.jl
100 lines (81 loc) · 2.65 KB
/
linqmacro.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module TestLinqMacro
using Test
using DataFrames
using DataFramesMeta
using Statistics
using Random
df = DataFrame(a = repeat(1:5, outer = 20),
b = repeat(["a", "b", "c", "d"], inner = 25),
x = repeat(1:20, inner = 5))
x = @where(df, :a .> 2, :b .!= "c")
x = @transform(x, :y = 10 * :x)
x = @orderby(x, :x .- mean(:x))
x = @by(x, :b, :meanX = mean(:x), :meanY = mean(:y))
x = @select(x, var = :b, :meanX, :meanY)
x1 = @linq transform(where(df, :a .> 2, :b .!= "c"), :y = 10 * :x)
x1 = @linq by(orderby(x1, :x .- mean(:x)), :b, :meanX = mean(:x), :meanY = mean(:y))
x1 = @linq select(x1, :var = :b, :meanX, :meanY)
## chaining
xlinq = @linq df |>
where(:a .> 2, :b .!= "c") |>
transform(:y = 10 * :x) |>
orderby(:x .- mean(:x)) |>
by(:b, :meanX = mean(:x), :meanY = mean(:y)) |>
select(:var = :b, :meanX, :meanY)
@test x == x1
@test x == xlinq
xlinq2 = @linq df |>
where(:a .> 2, :b .!= "c") |>
transform(:y = 10 * :x) |>
orderby(:x .- mean(:x)) |>
groupby(:b) |>
combine(:meanX = mean(:x), :meanY = mean(:y))
@test xlinq2[!, [:meanX, :meanY]] == xlinq[!, [:meanX, :meanY]]
xlinq3 = @linq df |>
where(:a .> 2, :b .!= "c") |>
transform(:y = 10 * :x) |>
orderby(:x .- mean(:x)) |>
DataFrames.groupby(:b) |>
combine(:meanX = mean(:x), :meanY = mean(:y))
@test xlinq3[!, [:meanX, :meanY]] == xlinq[!, [:meanX, :meanY]]
@test (@linq df |> with(:a)) == df.a
# commented out due to interpolation issues
# @testset "@linq with `$`" begin
df = DataFrame(
a = [1, 2, 3, 4],
b = ["a", "b", "c", "d"],
x = [10, 20, 30, 40],
y = [40, 50, 60, 70]
)
a_sym = :a
b_str = "b"
x_sym = :x
y_str = "y"
xlinq3 = @linq df |>
where($a_sym .> 2, :b .!= "c") |>
transform($y_str = 10 * $x_sym) |>
orderby($x_sym .- mean($x_sym)) |>
groupby(b_str) |>
combine($"meanX" = mean(:x), :meanY = mean(:y))
@test isequal(xlinq3, DataFrame(b = "d", meanX = 40.0, meanY = 400.0))
# end
@testset "@linq with `cols`" begin
df = DataFrame(
a = [1, 2, 3, 4],
b = ["a", "b", "c", "d"],
x = [10, 20, 30, 40],
y = [40, 50, 60, 70]
)
a_sym = :a
b_str = "b"
x_sym = :x
y_str = "y"
xlinq3 = @linq df |>
where(cols(a_sym) .> 2, :b .!= "c") |>
transform(cols(y_str) = 10 * cols(x_sym)) |>
orderby(cols(x_sym) .- mean(cols(x_sym))) |>
groupby(b_str) |>
combine(cols("meanX") = mean(:x), :meanY = mean(:y))
@test isequal(xlinq3, DataFrame(b = "d", meanX = 40.0, meanY = 400.0))
end
end # module