-
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.
Merge pull request #37 from ceferisbarov/coerce
Add Coerce
- Loading branch information
Showing
7 changed files
with
80 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
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 |
---|---|---|
|
@@ -37,6 +37,7 @@ export | |
DropMissing, | ||
Rename, | ||
Coalesce, | ||
Coerce, | ||
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,48 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
Coerce(pairs, tight=false, verbosity=1) | ||
Return a copy of the table, ensuring that the scientific types of the columns match the new specification. | ||
This transform wraps the ScientificTypes.coerce function. Please see their docstring for more details. | ||
```julia | ||
Coerce(:col1 => Continuous, :col2 => Count) | ||
``` | ||
""" | ||
struct Coerce{P} <: Transform | ||
pairs::P | ||
tight::Bool | ||
verbosity::Int | ||
end | ||
|
||
Coerce(pair::Pair{Symbol,<:Type}...; tight=false, verbosity=1) = | ||
Coerce(pair, tight, verbosity) | ||
|
||
isrevertible(::Type{<:Coerce}) = true | ||
|
||
function apply(transform::Coerce, table) | ||
newtable = coerce(table, transform.pairs...; | ||
tight=transform.tight, | ||
verbosity=transform.verbosity) | ||
|
||
types = Tables.schema(table).types | ||
|
||
newtable, types | ||
end | ||
|
||
function revert(transform::Coerce, newtable, cache) | ||
names = Tables.columnnames(newtable) | ||
cols = Tables.columns(newtable) | ||
oldcols = map(zip(cache, names)) do (T, n) | ||
x = Tables.getcolumn(cols, n) | ||
collect(T, x) | ||
end | ||
|
||
𝒯 = (; zip(names, oldcols)...) | ||
𝒯 |> Tables.materializer(newtable) | ||
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
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