-
Notifications
You must be signed in to change notification settings - Fork 15
Add StdNames #68
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
Merged
Merged
Add StdNames #68
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
30a6661
Add StdNames
d322959
Revised StdNames transform
488eeac
Revised StdNames transform and tests
84d065a
Update src/transforms/stdnames.jl
ceferisbarov b9f6150
Revised StdNames transform and tests
b401dab
Added tests for StdNames
9abca77
Update test/transforms.jl
ceferisbarov 4b13047
Revised StdNames transform and tests
ace0c43
Update src/transforms/stdnames.jl
ceferisbarov ff0c538
Revised StdNames
a81bcd5
Update Project.toml
juliohm 9bedf6d
Update Project.toml
juliohm aff91c1
Revised Rename transform to accept Dictionary of strings
155bf4f
Replaced rewrote _symboldict function in one line
d1564ab
Rewrote _symboldict function and added tests
6890774
Revised StdNames transform
21fef61
Merge branch 'JuliaML:master' into stdnames
ceferisbarov 96f4c5b
Update src/transforms/stdnames.jl
ceferisbarov 8e8abde
Update src/transforms/stdnames.jl
ceferisbarov 582e1cb
Update src/transforms/stdnames.jl
ceferisbarov f812f71
Update test/transforms.jl
ceferisbarov 983a49b
Revised StdNames
8e0f593
Added StdNames to docs
b060bce
Revised StdNames docstring and added tests
7e105b2
Updated _camel function
de48b72
Update src/transforms/stdnames.jl
ceferisbarov 76b500d
Added preprocessing step to StdNames
19c2e88
Update src/transforms/stdnames.jl
ceferisbarov 3251c75
Update src/transforms/stdnames.jl
ceferisbarov 1a5dcc0
Update src/transforms/stdnames.jl
ceferisbarov f42062c
Refactored StdNames and its tests
e5b085a
Update src/transforms/stdnames.jl
juliohm 50f3bf4
Update src/transforms/stdnames.jl
juliohm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -32,6 +32,12 @@ DropMissing | |
Rename | ||
``` | ||
|
||
## StdNames | ||
|
||
```@docs | ||
StdNames | ||
``` | ||
|
||
## Replace | ||
|
||
```@docs | ||
|
This file contains hidden or 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 | |
Filter, | ||
DropMissing, | ||
Rename, | ||
StdNames, | ||
Replace, | ||
Coalesce, | ||
Coerce, | ||
|
This file contains hidden or 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 hidden or 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,77 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
StdNames(spec) | ||
|
||
Standardizes column names according to given `spec`. | ||
Default to `:upper` case specification. | ||
|
||
# Specs | ||
|
||
* `:upper` - Uppercase, e.g. COLUMNNAME | ||
* `:camel` - Camelcase, e.g. ColumnName | ||
* `:snake` - Snakecase, e.g. column_name | ||
""" | ||
struct StdNames <: Stateless | ||
spec::Symbol | ||
end | ||
|
||
StdNames() = StdNames(:upper) | ||
|
||
isrevertible(::Type{StdNames}) = true | ||
|
||
function apply(transform::StdNames, table) | ||
# retrieve spec | ||
spec = transform.spec | ||
|
||
# retrieve column names | ||
cols = Tables.columns(table) | ||
oldnames = string.(Tables.columnnames(cols)) | ||
|
||
# clean column names | ||
cleaned = _clean.(oldnames) | ||
|
||
# apply spec | ||
spec == :camel && (names = _camel.(cleaned)) | ||
spec == :snake && (names = _snake.(cleaned)) | ||
spec == :upper && (names = _upper.(cleaned)) | ||
|
||
# make names unique | ||
newnames = _unique(names) | ||
|
||
# rename transform | ||
rtrans = Rename(Dict(oldnames .=> newnames)) | ||
newtable, rcache = apply(rtrans, table) | ||
|
||
newtable, (rtrans, rcache) | ||
end | ||
|
||
function revert(::StdNames, newtable, cache) | ||
rtrans, rcache = cache | ||
revert(rtrans, newtable, rcache) | ||
end | ||
|
||
const delim = [' ', '\t', '-', '_'] | ||
|
||
_clean(name) = filter(c -> isdigit(c) || isletter(c) || c ∈ delim, name) | ||
|
||
function _unique(names) | ||
newnames = String[] | ||
for name in names | ||
n = name | ||
while n ∈ newnames | ||
n = string(n, "_") | ||
end | ||
push!(newnames, n) | ||
end | ||
|
||
newnames | ||
end | ||
|
||
_camel(name) = join(uppercasefirst.(split(name, delim))) | ||
|
||
_snake(name) = join(lowercase.(split(strip(name, delim), delim)), '_') | ||
|
||
_upper(name) = replace(uppercase(name), delim => "") |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.