-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: implement Rename transform (#15)
* wip: implement Rename transform * wip: implement Rename transform * fix * Actually address brought up issues * refactor and add a pairs of strings constructor * add tests * confirm to Tables.jl spec * assert that all requested renames exist * update * make it work with a single Pair, and add test * Update src/transforms/rename.jl * Update src/transforms/rename.jl * Update src/transforms/rename.jl * Update src/transforms/rename.jl * Update src/transforms/rename.jl Co-authored-by: Júlio Hoffimann <[email protected]>
- Loading branch information
1 parent
a6d67c9
commit 97e2d77
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ export | |
# built-in | ||
Select, | ||
Reject, | ||
Rename, | ||
Identity, | ||
Center, | ||
Scale, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
Rename(:col₁ => :newcol₁, :col₂ => :newcol₂, ..., :col₁ => :newcolₙ) | ||
The transform that renames `col₁` to `newcol₁`, `col₂` to `newcol₂`, ... | ||
""" | ||
struct Rename <: Stateless | ||
names::Dict{Symbol,Symbol} | ||
end | ||
|
||
pairsyms(x::Pair) = Symbol(first(x)) => Symbol(last(x)) | ||
|
||
Rename(names::Pair) = pairsyms(names) |> Dict |> Rename | ||
Rename(names...) = pairsyms.(names) |> Dict |> Rename | ||
|
||
function apply(transform::Rename, table) | ||
_rename(transform.names, table) | ||
end | ||
|
||
function revert(transform::Rename, table, cache) | ||
# reversing the key-value pairs of the Dict | ||
newnames = Dict(new => old for (old, new) in transform.names) | ||
_rename(newnames, table) |> first | ||
end | ||
|
||
|
||
function _rename(names, table) | ||
oldnames = Tables.columnnames(table) | ||
|
||
# check if requested renames exist in the table | ||
@assert keys(names) ⊆ oldnames "invalid column names" | ||
|
||
# use new names if necessary | ||
newnames = map(oldnames) do oldname | ||
oldname in keys(names) ? names[oldname] : oldname | ||
end | ||
|
||
cols = Tables.columns(table) | ||
vals = [Tables.getcolumn(cols, name) for name in oldnames] | ||
𝒯 = (; zip(newnames, vals)...) |> Tables.materializer(table) | ||
|
||
𝒯, nothing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters