-
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 #42 from eliascarv/replace
Add Replace
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -36,6 +36,7 @@ export | |
Filter, | ||
DropMissing, | ||
Rename, | ||
Replace, | ||
Coalesce, | ||
Coerce, | ||
Identity, | ||
|
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,28 @@ | ||
# ------------------------------------------------------------------ | ||
# Licensed under the MIT License. See LICENSE in the project root. | ||
# ------------------------------------------------------------------ | ||
|
||
""" | ||
Replace(old₁ => new₁, old₂ => new₂, ..., oldₙ => newₙ) | ||
Replaces `oldᵢ` value with `newᵢ` value in the table. | ||
""" | ||
struct Replace{K,V} <: Colwise | ||
pairs::IdDict{K,V} | ||
end | ||
|
||
Replace() = throw(ArgumentError("Cannot create a Replace object without arguments.")) | ||
|
||
Replace(pairs::Pair...) = Replace(IdDict(values(pairs))) | ||
|
||
isrevertible(::Type{<:Replace}) = true | ||
|
||
function colcache(transform::Replace, x) | ||
olds = keys(transform.pairs) | ||
inds = [findall(v -> v === old, x) .=> old for old in olds] | ||
Dict(reduce(vcat, inds)) | ||
end | ||
|
||
colapply(transform::Replace, x, c) = [get(transform.pairs, xᵢ, xᵢ) for xᵢ in x] | ||
|
||
colrevert(transform::Replace, x, c) = [get(c, i, x[i]) for i in 1:length(x)] |
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