-
Notifications
You must be signed in to change notification settings - Fork 55
/
rename.jl
184 lines (130 loc) · 6.19 KB
/
rename.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
module TestRename
using Test
using DataFrames
using DataFramesMeta
using Statistics
const ≅ = isequal
@testset "rename" begin
df = DataFrame(old_col1 = rand(10), old_col2 = rand([missing;2:10],10),old_col3 = rand(10));
# rename test set
@test @rename(df, :new1 = :old_col1) ≅ rename(df, :old_col1 => :new1)
@test @rename(df, :new1 = $"old_col1", :new2 = $"old_col2") ≅ rename(df, [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename(df, :new1 = :old_col1, :new2 = :old_col2) ≅ rename(df, [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename(df, :new1 = :old_col1, :new2 = $"old_col2") ≅ rename(df, [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename(df, :new1 = $("old_col" * "1"), :new2 = :old_col2) ≅ rename(df, [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename(df, $("old_col1" => "new1") ) ≅ rename(df, :old_col1 .=> :new1 )
@test @rename(df, $(:old_col1 => :new1)) ≅ rename(df, :old_col1 .=> :new1 )
@test @rename(df, $(:old_col1 => "new1"), $(:old_col2 => :new2) ) ≅ rename(df, [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename(df, $(:old_col1 => "new1"), :new2 = :old_col2) ≅ rename(df, [:old_col1, :old_col2] .=> [:new1,:new2] )
res = @rename df :new1 = $(begin
"old_col" * "1"
end)
@test res ≅ rename(df, :old_col1 .=> :new1 )
@test @rename(df, :new1 = $1)≅ rename(df, 1=>:new1)
@test @rename(df, $(1=> :new1 )) ≅ rename(df, 1=>:new1)
@test @rename(df, $"new1" = :old_col1) ≅ rename(df, :old_col1=>:new1)
@test @rename(df, $("new" * "1") = :old_col1) ≅ rename(df, :old_col1=>:new1)
@test @rename(df, $("new" * "1") = $"old_col1") ≅ rename(df, :old_col1=>:new1)
end
@testset "rename with :block" begin
df = DataFrame(old_col1 = rand(10), old_col2 = rand(10),old_col3 = rand(10));
res = @rename df begin
:new1 = :old_col1
:new2 = :old_col2
:new3 = $"old_col3"
end
@test res ≅ @rename(df, :new1 =:old_col1, :new2 = :old_col2, :new3 = :old_col3)
res = @rename copy(df) begin
:new1 = $"old_col1"
:new2 = $"old_col2"
end
@test res ≅ @rename(df, :new1 =:old_col1, :new2 = :old_col2)
res = @rename df :new1 = $(begin
"old_col" * "1"
end)
@test res ≅ @rename(df, :new1 =:old_col1)
res = @rename df begin
$("old_col" * "1" => String(:new1))
end
@test res ≅ @rename(df, :new1 = :old_col1)
subdf = @view df[df.old_col1 .< .5, :]
res = @rename copy(subdf) begin
:new1 = :old_col1
end
@test res ≅ @rename(subdf, :new1 = :old_col1)
res = @rename df begin
:new1 = $1
end
@test res≅ @rename(df, :new1 = $1)
res = @rename df begin
$(1 => :new1)
end
@test res ≅ @rename(df, :new1 = $1)
end
@testset "rename!" begin
df = DataFrame(old_col1 = rand(10), old_col2 = rand(10),old_col3 = rand(10));
# rename test set
@test @rename!(copy(df), :new1 = :old_col1) ≅ rename(copy(df), :old_col1 => :new1)
@test @rename!(copy(df), :new1 = $"old_col1", :new2 = $"old_col2") ≅ rename(copy(df), [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename!(copy(df), :new1 = :old_col1, :new2 = :old_col2) ≅ rename(copy(df), [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename!(copy(df), :new1 = :old_col1, :new2 = $"old_col2") ≅ rename(copy(df), [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename!(copy(df), :new1 = $("old_col" *"1"), :new2 = :old_col2) ≅ rename(copy(df), [:old_col1, :old_col2] .=> [:new1,:new2] )
@test @rename!(copy(df), $("old_col1" => "new1") ) ≅ rename(copy(df), :old_col1 .=> :new1 )
@test @rename!(copy(df), $(:old_col1 => :new1)) ≅ rename(copy(df), :old_col1 .=> :new1 )
@test @rename!(copy(df), $(:old_col1 => "new1"), $(:old_col2 => :new2)) ≅ rename(copy(df), [:old_col1,:old_col2] .=> [:new1,:new2] )
@test @rename!(copy(df), $(:old_col1 => "new1"), :new2 = :old_col2) ≅ rename(copy(df), [:old_col1, :old_col2] .=> [:new1,:new2] )
res = @rename! copy(df) :new1 = $(begin
("old_col" * "1")
end)
@test res ≅ rename!(copy(df), :old_col1 .=> :new1 )
@test @rename!(copy(df), :new1 = $1) ≅ rename(copy(df), 1=>:new1)
@test @rename!(copy(df), $(1=> :new1 )) ≅ rename(copy(df), 1=>:new1)
@test @rename(df, $"new1" = :old_col1) ≅ rename(df, :old_col1=>:new1)
@test @rename(df, $("new" * "1") = :old_col1) ≅ rename(df, :old_col1=>:new1)
@test @rename(df, $("new" * "1") = $"old_col1") ≅ rename(df, :old_col1=>:new1)
end
@testset "rename! with :block" begin
df = DataFrame(old_col1 = rand(10), old_col2 = rand(10), old_col3 = rand(10));
res = @rename! copy(df) begin
:new1 = :old_col1
:new2 = :old_col2
:new3 = $"old_col3"
end
@test res ≅ @rename!(copy(df), :new1 =:old_col1, :new2 = :old_col2, :new3 = :old_col3)
res = @rename! copy(df) begin
:new1 = $"old_col1"
:new2 = $"old_col2"
end
@test res ≅ @rename!(copy(df), :new1 =:old_col1, :new2 = :old_col2)
res = @rename! copy(df) begin
:new1 = $("old_col" * "1")
end
@test res ≅ @rename!(copy(df), :new1 =:old_col1)
res = @rename! copy(df) begin
$("old_col" * "1" => string(:new1))
end
@test res ≅ @rename!(copy(df), :new1 = :old_col1)
subdf = @view df[df.old_col1 .< .5, :]
d = @rename! copy(subdf) begin
:new1 = :old_col1
end
@test d ≅ @rename!(copy(subdf), :new1 = :old_col1)
res = @rename! copy(df) begin
:new1 = $1
end
@test res≅ @rename!(copy(df), :new1 = $1)
res = @rename! copy(df) begin
$(1=> :new1 )
end
@test res ≅ @rename!(copy(df), :new1 = $1)
end
df = DataFrame(x = 1)
@testset "rename errors" begin
@test_throws LoadError @eval @rename df $1 = :x
@test_throws LoadError @eval @rename df $(1.1) = :x
@test_throws LoadError @eval @rename df :y = $(1.1)
@test_throws LoadError @eval @rename df :y = AsTable(:y)
@test_throws LoadError @eval @rename df "y" = :x
@test_throws LoadError @eval @rename df :y = "x"
end
end # module