Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
30a6661
Add StdNames
May 5, 2022
d322959
Revised StdNames transform
May 6, 2022
488eeac
Revised StdNames transform and tests
May 7, 2022
84d065a
Update src/transforms/stdnames.jl
ceferisbarov May 7, 2022
b9f6150
Revised StdNames transform and tests
May 7, 2022
b401dab
Added tests for StdNames
May 7, 2022
9abca77
Update test/transforms.jl
ceferisbarov May 9, 2022
4b13047
Revised StdNames transform and tests
May 9, 2022
ace0c43
Update src/transforms/stdnames.jl
ceferisbarov May 9, 2022
ff0c538
Revised StdNames
May 9, 2022
a81bcd5
Update Project.toml
juliohm May 5, 2022
9bedf6d
Update Project.toml
juliohm May 5, 2022
aff91c1
Revised Rename transform to accept Dictionary of strings
May 10, 2022
155bf4f
Replaced rewrote _symboldict function in one line
May 10, 2022
d1564ab
Rewrote _symboldict function and added tests
May 10, 2022
6890774
Revised StdNames transform
May 10, 2022
21fef61
Merge branch 'JuliaML:master' into stdnames
ceferisbarov May 10, 2022
96f4c5b
Update src/transforms/stdnames.jl
ceferisbarov May 10, 2022
8e8abde
Update src/transforms/stdnames.jl
ceferisbarov May 10, 2022
582e1cb
Update src/transforms/stdnames.jl
ceferisbarov May 10, 2022
f812f71
Update test/transforms.jl
ceferisbarov May 10, 2022
983a49b
Revised StdNames
May 10, 2022
8e0f593
Added StdNames to docs
May 10, 2022
b060bce
Revised StdNames docstring and added tests
May 11, 2022
7e105b2
Updated _camel function
May 11, 2022
de48b72
Update src/transforms/stdnames.jl
ceferisbarov May 11, 2022
76b500d
Added preprocessing step to StdNames
May 12, 2022
19c2e88
Update src/transforms/stdnames.jl
ceferisbarov May 16, 2022
3251c75
Update src/transforms/stdnames.jl
ceferisbarov May 16, 2022
1a5dcc0
Update src/transforms/stdnames.jl
ceferisbarov May 16, 2022
f42062c
Refactored StdNames and its tests
May 16, 2022
e5b085a
Update src/transforms/stdnames.jl
juliohm May 17, 2022
50f3bf4
Update src/transforms/stdnames.jl
juliohm May 17, 2022
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
Filter,
DropMissing,
Rename,
StdNames,
Replace,
Coalesce,
Coerce,
Expand Down
1 change: 1 addition & 0 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ end
include("transforms/select.jl")
include("transforms/filter.jl")
include("transforms/rename.jl")
include("transforms/stdnames.jl")
include("transforms/replace.jl")
include("transforms/coalesce.jl")
include("transforms/coerce.jl")
Expand Down
59 changes: 59 additions & 0 deletions src/transforms/stdnames.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

"""
StdNames(S)
StdNames transform standardizes the column names.
"""
struct StdNames <: Stateless
stdmethod::Symbol
end

isrevertible(::Type{StdNames}) = true

function apply(transform::StdNames, table)
if transform.stdmethod == :camel
_camel(table)
elseif transform.stdmethod == :snake
_snake(table)
else
_upper(table)
end
end

function revert(::StdNames, newtable, cache)
names = Tables.columnnames(newtable)
namesdict = Dict(zip(names, cache))
newtable |> Rename(namesdict)
end

function _camel(table)
oldnames = Tables.columnnames(table)
function camelfunction(x)
substrings = split(string(x))
capitalize(s) = String([i == 1 ? uppercase(c) : c for (i, c) in enumerate(s)])
Symbol(join(map(capitalize, substrings)))
end

newnames = map(camelfunction, oldnames)
namesdict = Dict(zip(oldnames, newnames))
table |> Rename(namesdict), oldnames
end

function _snake(table)
oldnames = Tables.columnnames(table)
snakefunction(x) = Symbol(join(split(string(x)), "_"))
newnames = map(snakefunction, oldnames)
namesdict = Dict(zip(oldnames, newnames))
table |> Rename(namesdict), oldnames
end

function _upper(table)
oldnames = Tables.columnnames(table)
upperfunction(x) = Symbol(uppercase(string(x)))
newnames = map(upperfunction, oldnames)
namesdict = Dict(zip(oldnames, newnames))
table |> Rename(namesdict), oldnames
end
28 changes: 28 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,34 @@
@test n1 == n2
end

@testset "StdNames" begin
a = [3, 2, 1, 4, 5, 3]
b = [2, 4, 4, 5, 8, 5]
t = Table(; a, b)
t = t |> Rename(:a => "apple tree", :b => "banana tree")

# upper test
T = StdNames(:upper)
n, c = apply(T, t)
tₒ = revert(T, n, c)
@test Tables.columnnames(n) == (Symbol("APPLE TREE"), Symbol("BANANA TREE"))
@test t == tₒ

# camel test
T = StdNames(:camel)
n, c = apply(T, t)
tₒ = revert(T, n, c)
@test Tables.columnnames(n) == (:AppleTree, :BananaTree)
@test t == tₒ

# snake test
T = StdNames(:snake)
n, c = apply(T, t)
tₒ = revert(T, n, c)
@test Tables.columnnames(n) == (:apple_tree, :banana_tree)
@test t == tₒ
end

@testset "Replace" begin
a = [3, 2, 1, 4, 5, 3]
b = [2, 4, 4, 5, 8, 5]
Expand Down