Skip to content

Commit f435fd2

Browse files
authored
Add support for vector of pairs in Rename transform (#261)
* Add support for vector of pairs in 'Rename' transform * Update tests
1 parent c7b2fcf commit f435fd2

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/transforms/rename.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
"""
66
Rename(:col₁ => :newcol₁, :col₂ => :newcol₂, ..., :colₙ => :newcolₙ)
7+
Rename([:col₁ => :newcol₁, :col₂ => :newcol₂, ..., :colₙ => :newcolₙ])
78
89
The transform that renames `col₁`, `col₂`, ..., `colₙ`
910
to `newcol₁`, `newcol₂`, ..., `newcolₙ`.
@@ -14,6 +15,9 @@ to `newcol₁`, `newcol₂`, ..., `newcolₙ`.
1415
Rename(1 => :x, 3 => :y)
1516
Rename(:a => :x, :c => :y)
1617
Rename("a" => "x", "c" => "y")
18+
Rename([1 => "x", 3 => "y"])
19+
Rename([:a => "x", :c => "y"])
20+
Rename(["a", "c"] .=> [:x, :y])
1721
```
1822
"""
1923
struct Rename{S<:ColumnSelector} <: StatelessFeatureTransform
@@ -30,6 +34,11 @@ Rename(pairs::Pair{C,Symbol}...) where {C<:Column} = Rename(selector(first.(pair
3034
Rename(pairs::Pair{C,S}...) where {C<:Column,S<:AbstractString} =
3135
Rename(selector(first.(pairs)), collect(Symbol.(last.(pairs))))
3236

37+
Rename(pairs::AbstractVector{Pair{C,Symbol}}) where {C<:Column} = Rename(selector(first.(pairs)), last.(pairs))
38+
39+
Rename(pairs::AbstractVector{Pair{C,S}}) where {C<:Column,S<:AbstractString} =
40+
Rename(selector(first.(pairs)), Symbol.(last.(pairs)))
41+
3342
isrevertible(::Type{<:Rename}) = true
3443

3544
function applyfeat(transform::Rename, feat, prep)

test/transforms/rename.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,43 @@
109109
n2 = reapply(T, t, c1)
110110
@test n1 == n2
111111

112+
# vector of pairs
113+
T = Rename([1 => :x, 3 => :y])
114+
n, c = apply(T, t)
115+
@test Tables.schema(n).names == (:x, :b, :y, :d)
116+
tₒ = revert(T, n, c)
117+
@test t == tₒ
118+
119+
T = Rename([2, 4] .=> ["x", "y"])
120+
n, c = apply(T, t)
121+
@test Tables.schema(n).names == (:a, :x, :c, :y)
122+
tₒ = revert(T, n, c)
123+
@test t == tₒ
124+
125+
T = Rename([:a => :x, :c => :y])
126+
n, c = apply(T, t)
127+
@test Tables.schema(n).names == (:x, :b, :y, :d)
128+
tₒ = revert(T, n, c)
129+
@test t == tₒ
130+
131+
T = Rename([:b, :d] .=> ["x", "y"])
132+
n, c = apply(T, t)
133+
@test Tables.schema(n).names == (:a, :x, :c, :y)
134+
tₒ = revert(T, n, c)
135+
@test t == tₒ
136+
137+
T = Rename(["a" => :x, "c" => :y])
138+
n, c = apply(T, t)
139+
@test Tables.schema(n).names == (:x, :b, :y, :d)
140+
tₒ = revert(T, n, c)
141+
@test t == tₒ
142+
143+
T = Rename(["b", "d"] .=> ["x", "y"])
144+
n, c = apply(T, t)
145+
@test Tables.schema(n).names == (:a, :x, :c, :y)
146+
tₒ = revert(T, n, c)
147+
@test t == tₒ
148+
112149
# throws
113150
@test_throws AssertionError Rename(:a => :x, :b => :x)
114151
@test_throws AssertionError apply(Rename(:a => :c, :b => :d), t)

0 commit comments

Comments
 (0)