-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathdeprecated.jl
201 lines (160 loc) · 6.09 KB
/
deprecated.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
module Deprecated
using Test
using DataFramesMeta
using Statistics
const ≅ = isequal
@testset "@based_on" 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"
gd = groupby(df, :g)
n_str = "new_column"
n_sym = :new_column
n_space = "new column"
@test @based_on(gd, n = mean(:i)).n == [2.0, 4.5]
@test @based_on(gd, n = mean(:i) + mean(:g)).n == [3.0, 6.5]
@test @based_on(gd, n = first(:t .* string.(:y))).n == ["av", "cy"]
@test @based_on(gd, n = first(Symbol.(:y, ^(:t)))).n == [:vt, :yt]
@test @based_on(gd, n = first(Symbol.(:y, ^(:body)))).n == [:vbody, :ybody]
@test @based_on(gd, body = :i).body == df.i
@test @based_on(gd, transform = :i).transform == df.i
@test @based_on(gd, n = mean(cols(iq))).n == [2.0, 4.5]
@test @based_on(gd, n = mean(cols(iq)) + mean(cols(gq))).n == [3.0, 6.5]
@test @based_on(gd, n = first(cols(tq) .* string.(cols(yq)))).n == ["av", "cy"]
@test @based_on(gd, n = first(Symbol.(cols(yq), ^(:t)))).n == [:vt, :yt]
@test @based_on(gd, n = first(Symbol.(cols(yq), ^(:body)))).n == [:vbody, :ybody]
@test @based_on(gd, body = cols(iq)).body == df.i
@test @based_on(gd, transform = cols(iq)).transform == df.i
@test @based_on(gd, n = mean(cols(ir))).n == [2.0, 4.5]
@test @based_on(gd, n = mean(cols(ir)) + mean(cols(gr))).n == [3.0, 6.5]
@test @based_on(gd, n = first(cols(tr) .* string.(cols(yr)))).n == ["av", "cy"]
@test @based_on(gd, n = first(Symbol.(cols(yr), ^(:t)))).n == [:vt, :yt]
@test @based_on(gd, n = first(Symbol.(cols(yr), ^(:body)))).n == [:vbody, :ybody]
@test @based_on(gd, body = cols(ir)).body == df.i
@test @based_on(gd, transform = cols(ir)).transform == df.i
@test @based_on(gd, n = mean(cols("i")) + 0 * first(cols(:g))).n == [2.0, 4.5]
@test @based_on(gd, n = mean(cols(2)) + first(cols(1))).n == [3.0, 6.5]
@test @based_on(gd, :i) == select(df, :g, :i)
@test @based_on(gd, :i, :g) ≅ select(df, :g, :i)
@test @based_on(gd, :i, n = 1).n == fill(1, nrow(df))
@test @based_on(gd, cols("new_column") = 2).new_column == [2, 2]
@test @based_on(gd, cols(n_str) = 2).new_column == [2, 2]
@test @based_on(gd, cols(n_sym) = 2).new_column == [2, 2]
@test @based_on(gd, cols(n_space) = 2)."new column" == [2, 2]
@test @based_on(gd, cols("new" * "_" * "column") = 2)."new_column" == [2, 2]
end
@testset "where" begin
df = DataFrame(A = [1, 2, 3, missing], B = [2, 1, 2, 1])
x = [2, 1, 0, 0]
@test @where(df, :A .> 1) == df[(df.A .> 1) .=== true,:]
@test @where(df, :B .> 1) == df[df.B .> 1,:]
@test @where(df, :A .> x) == df[(df.A .> x) .=== true,:]
@test @where(df, :B .> x) ≅ df[df.B .> x,:]
@test @where(df, :A .> :B, :B .> mean(:B)) == DataFrame(A = 3, B = 2)
@test @where(df, :A .> 1, :B .> 1) == df[map(&, df.A .> 1, df.B .> 1),:]
@test @where(df, :A .> 1, :A .< 4, :B .> 1) == df[map(&, df.A .> 1, df.A .< 4, df.B .> 1),:]
@test @where(df, :A .> 1).A isa Vector{Union{Missing, Int}}
@test @where(df, cols(:A) .> 1) == df[(df.A .> 1) .=== true,:]
@test @where(df, cols(:B) .> 1) == df[df.B .> 1,:]
@test @where(df, cols(:A) .> x) == df[(df.A .> x) .=== true,:]
@test @where(df, cols(:B) .> x) ≅ df[df.B .> x,:]
@test @where(df, cols(:A) .> :B, cols(:B) .> mean(:B)) == DataFrame(A = 3, B = 2)
@test @where(df, cols(:A) .> 1, :B .> 1) == df[map(&, df.A .> 1, df.B .> 1),:]
@test @where(df, cols(:A) .> 1, :A .< 4, :B .> 1) == df[map(&, df.A .> 1, df.A .< 4, df.B .> 1),:]
@test @where(df, :A .> 1, :A .<= 2) == DataFrame(A = 2, B = 1)
subdf = @view df[df.B .== 2, :]
@test @where(subdf, :A .== 3) == DataFrame(A = 3, B = 2)
end
@testset "where with :block" begin
df = DataFrame(A = [1, 2, 3, missing], B = [2, 1, 2, 1])
d = @where df begin
:A .> 1
:B .> 1
end
@test d ≅ @where(df, :A .> 1, :B .> 1)
d = @where df begin
cols(:A) .> 1
:B .> 1
end
@test d ≅ @where(df, :A .> 1, :B .> 1)
d = @where df begin
:A .> 1
cols(:B) .> 1
end
@test d ≅ @where(df, :A .> 1, :B .> 1)
d = @where df begin
begin
:A .> 1
end
:B .> 1
end
@test d ≅ @where(df, :A .> 1, :B .> 1)
d = @where df begin
:A .> 1
@. :B > 1
end
@test d ≅ @where(df, :A .> 1, :B .> 1)
end
@testset "@where with a grouped data frame" 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]
)
gd = groupby(df, :g)
@test @where(gd, :i .== first(:i)) ≅ df[[1, 4], :]
@test @where(gd, cols(:i) .> mean(cols(:i)), :t .== "c") ≅ df[[3], :]
@test @where(gd, :c .== :g) ≅ df[[], :]
end
@testset "Unquoted symbols on LHS" 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]
)
gd = groupby(df, :g)
newdf = @transform df :n = :i
@test (@transform df n = :i) ≅ newdf
@test (@transform(df, n = identity(:i))) ≅ newdf
@test (@transform df @byrow n = :i) ≅ newdf
d = @transform df begin
n = identity(:i)
end
@test d ≅ newdf
d = @eachrow df begin
@newcol n::Vector{Int}
:n = :i
end
@test d ≅ newdf
newdf = @select df :n = :i
@test (@select df n = :i) ≅ newdf
@test (@select(df, n = identity(:i))) ≅ newdf
d = @select df begin
n = identity(:i)
end
@test (@select df @byrow n = :i) ≅ newdf
@test d ≅ newdf
newdf = @combine gd :n = first(:i)
@test (@combine gd n = first(:i)) ≅ newdf
@test (@combine(gd, n = first(:i))) ≅ newdf
end
end # module