Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Dec 12, 2023
1 parent d4f4db9 commit 71a29c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/transforms/unitify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Add units to columns of the table using bracket syntax.
A column named `col [unit]` will be renamed to a unitful
column `col` with a valid `unit` from Unitful.jl.
In the case that the `unit` is not recognized by Unitful.jl,
no units are added. Empty brackets are also allowed to represent
columns without units, e.g. `col []`.
"""
struct Unitify <: StatelessFeatureTransform end

Expand All @@ -18,10 +22,14 @@ function _unitify(name)
if !isnothing(m)
namestr, unitstr = m.captures
newname = Symbol(strip(namestr))
unit = try
uparse(unitstr)
catch
@warn "the unit \"$unitstr\" is not valid"
unit = if !isempty(unitstr)
try
uparse(unitstr)
catch
@warn "the unit \"$unitstr\" is not valid"
NoUnits
end
else
NoUnits
end
newname, unit
Expand Down
10 changes: 10 additions & 0 deletions test/transforms/unitify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
tₒ = revert(T, n, c)
@test tₒ == t

# no units
t = Table(; Symbol("a []") => rand(10), Symbol("b [NoUnits]") => rand(10))
T = Unitify()
n, c = apply(T, t)
@test Tables.schema(n).names == (:a, :b)
@test unit(eltype(n.a)) === NoUnits
@test unit(eltype(n.b)) === NoUnits
tₒ = revert(T, n, c)
@test tₒ == t

# invalid unit name
t = Table(; Symbol("a [test]") => rand(10))
T = Unitify()
Expand Down

0 comments on commit 71a29c9

Please sign in to comment.