-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy patheachrow.jl
225 lines (179 loc) · 4.62 KB
/
eachrow.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
module Testeachrow
using Test
using DataFrames
using DataFramesMeta
using Statistics
const ≅ = isequal
# `y` needs to be in global scope here because the testset relies on `y`
y = 0
@testset "eachrow" begin
df = DataFrame(A = 1:3, B = [2, 1, 2])
@test @eachrow(df, if :A > :B; :A = 0 end) == DataFrame(A = [1, 0, 0], B = [2, 1, 2])
@test df == DataFrame(A = [1, 2, 3], B = [2, 1, 2])
df = DataFrame(A = 1:3, B = [2, 1, 2]) # Restore df
@eachrow(df, if :A + :B == 3; global y += 1 end)
@test y == 2
df = DataFrame(A = 1:3, B = [2, 1, 2])
df2 = @eachrow df begin
@newcol :colX::Array{Float64}
@newcol :colY::Array{Float64}
:colX = :B == 2 ? pi * :A : :B
if :A > 1
:colY = :A * :B
end
end
@test df2.colX == [pi, 1.0, 3pi]
@test df2[2, :colY] == 2
df = DataFrame(b = [5], a = [(n1 = 1, n2 = 2)])
df2 = @eachrow df begin
:b = :a.n1
end
@test df2.b == [1]
_DF = 5
_N = 0
@eachrow df begin
# nothing
end
@test _DF == 5
@test _N == 0
df3 = @eachrow df begin
:b = row
end
@test df3.b == [1]
x = (a = 400, b = 600)
df4 = @eachrow df begin
:b = x.a
end
@test df4.b == [400]
end
@testset "cols with @eachrow" begin
df = DataFrame(A = 1:3, B = [2, 1, 2])
n = :A
df2 = @eachrow df begin
:B = $n
end
@test df2 == DataFrame(A = 1:3, B = 1:3)
n = "A"
df2 = @eachrow df begin
:B = $n
end
@test df2 == DataFrame(A = 1:3, B = 1:3)
df2 = @eachrow df begin
:A = $:A + $"B"
end
@test df2.A == df.A + df.B
n = :A
df2 = @eachrow df begin
$n = :B
end
@test df2 == DataFrame(A = [2, 1, 2], B = [2, 1, 2])
n = "A"
df2 = @eachrow df begin
$n = :B
end
@test df2 == DataFrame(A = [2, 1, 2], B = [2, 1, 2])
n = :C
df2 = @eachrow df begin
@newcol $n::Vector{Int}
$n = :B
end
@test df2 == DataFrame(A = [1, 2, 3], B = [2, 1, 2], C = [2, 1, 2])
n = "C"
df2 = @eachrow df begin
@newcol $n::Vector{Int}
$n = :B
end
@test df2 == DataFrame(A = [1, 2, 3], B = [2, 1, 2], C = [2, 1, 2])
end
df = DataFrame(A = 1:3, B = [2, 1, 2])
@testset "limits of @eachrow" begin
@eval Testeachrow n = ["A", "B"]
@test_throws ArgumentError @eval @eachrow df begin cols(n) end
@eval Testeachrow n = [:A, :B]
@test_throws ArgumentError @eval @eachrow df begin cols(n) end
@eval Testeachrow n = [1, 2]
@test_throws ArgumentError @eval @eachrow df begin cols(n) end
@test_throws ArgumentError @eachrow df $1 + $:A
end
# `y` needs to be in global scope here because the testset relies on `y`
y = 0
@testset "eachrow!" begin
df = DataFrame(A = 1:3, B = [2, 1, 2])
@test @eachrow!(df, if :A > :B; :A = 0 end) === df
@test df == DataFrame(A = [1, 0, 0], B = [2, 1, 2])
@test df == DataFrame(A = [1, 0, 0], B = [2, 1, 2])
df = DataFrame(A = 1:3, B = [2, 1, 2]) # Restore df
@eachrow!(df, if :A + :B == 3; global y += 1 end)
@test y == 2
df = DataFrame(A = 1:3, B = [2, 1, 2])
df2 = @eachrow! df begin
@newcol :colX::Array{Float64}
@newcol :colY::Array{Float64}
:colX = :B == 2 ? pi * :A : :B
if :A > 1
:colY = :A * :B
end
end
@test df.colX == [pi, 1.0, 3pi]
@test df[2, :colY] == 2
@test df2 === df2
df = DataFrame(b = [5], a = [(n1 = 1, n2 = 2)])
@eachrow! df begin
:b = :a.n1
end
@test df.b == [1]
_DF = 5
_N = 0
@eachrow df begin
# nothing
end
@test _DF == 5
@test _N == 0
df = DataFrame(b = [5], a = [(n1 = 1, n2 = 2)])
@eachrow! df begin
:b = row
end
@test df.b == [1]
df = DataFrame(b = [5], a = [(n1 = 1, n2 = 2)])
x = (a = 400, b = 600)
@eachrow! df begin
:b = x.a
end
@test df.b == [400]
end
@testset "eachrow with getproperty" begin
df = DataFrame(a = [(x = 1, y = 2), (x = 3, y = 4)], b = 1:2)
res = @eachrow df begin
:b = :a.x + :b
end
@test res.b == [2, 5]
res = @eachrow df begin
:b = $"a".x + :b
end
@test res.b == [2, 5]
end
@testset "#334" begin
df = DataFrame(x = [1, 2, 3])
s = Ref(0)
@eachrow df begin
continue
s[] = 100
end
@test s[] == 0
s = Ref(0)
@eachrow df begin
for i in 1:10
continue
s[] = 100
end
s[] = s[] + 1
end
@test s[] == 3
s = Ref(0)
@eachrow df begin
break
s[] = 100
end
@test s[] == 0
end
end # module