Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Rename transform #15

Merged
merged 16 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/TableTransforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export
# built-in
Select,
Reject,
Rename,
Identity,
Center,
Scale,
Expand Down
1 change: 1 addition & 0 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ end

include("transforms/identity.jl")
include("transforms/select.jl")
include("transforms/rename.jl")
include("transforms/center.jl")
include("transforms/scale.jl")
include("transforms/zscore.jl")
Expand Down
37 changes: 37 additions & 0 deletions src/transforms/rename.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

"""
Rename(:col₁ => :newcol₁, :col₂ => :newcol₂, ..., :col₁ => :newcolₙ)
juliohm marked this conversation as resolved.
Show resolved Hide resolved

The transform that renames `col₁` to `newcol₁`, `col₂` to `newcol₂`, ...
"""
struct Rename <: Stateless
names::Dict{Symbol,Symbol}
end

function apply(transform::Rename, table)
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
oldnames = Tables.columnnames(table)
newnames = map(oldnames) do oldname
if oldname in keys(transform.names)
transform.names[oldname]
else
oldname
end
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
end
acols = [i for i in Tables.columns(table)]
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
𝒯 = (; zip(newnames, acols)...) |> Tables.materializer(table)
𝒯, nothing
end

function revert(transform::Rename, table)
# reversing the key-value pairs of the Dict
new_names = Dict()
for (new, old) in transform.names
Omar-Elrefaei marked this conversation as resolved.
Show resolved Hide resolved
new_names[old] = new
end

reversed_transform = Rename(new_names)
apply(reversed_transform, table) |> first
juliohm marked this conversation as resolved.
Show resolved Hide resolved
end