5
5
"""
6
6
Unit(unit)
7
7
8
- Converts the units of all columns in the table to `unit`.
8
+ Converts the units of all columns in the table to `unit` from Unitful.jl .
9
9
10
10
Unit(cols₁ => unit₁, cols₂ => unit₂, ..., colsₙ => unitₙ)
11
11
12
12
Converts the units of selected columns `cols₁`, `cols₂`, ..., `colsₙ`
13
13
to `unit₁`, `unit₂`, ... `unitₙ`.
14
14
15
- The column selection can be a single column identifier (index or name),
16
- a collection of identifiers or a regular expression (regex).
15
+ Unitless columns become unitful if they are explicitly selected.
17
16
18
17
# Examples
19
18
@@ -39,10 +38,6 @@ Unit(pairs::Pair...) = Unit(collect(selector.(first.(pairs))), collect(last.(pai
39
38
40
39
isrevertible (:: Type{<:Unit} ) = true
41
40
42
- _uconvert (u, x) = _uconvert (nonmissingtype (eltype (x)), u, x)
43
- _uconvert (:: Type , _, x) = (x, nothing )
44
- _uconvert (:: Type{Q} , u, x) where {Q<: AbstractQuantity } = (map (v -> uconvert (u, v), x), unit (Q))
45
-
46
41
function applyfeat (transform:: Unit , feat, prep)
47
42
cols = Tables. columns (feat)
48
43
names = Tables. columnnames (cols)
@@ -59,7 +54,7 @@ function applyfeat(transform::Unit, feat, prep)
59
54
x = Tables. getcolumn (cols, name)
60
55
if haskey (unitdict, name)
61
56
u = unitdict[name]
62
- _uconvert (u, x)
57
+ _withunit (u, x)
63
58
else
64
59
(x, nothing )
65
60
end
@@ -80,9 +75,22 @@ function revertfeat(::Unit, newfeat, fcache)
80
75
ounits = fcache
81
76
columns = map (names, ounits) do name, u
82
77
x = Tables. getcolumn (cols, name)
83
- isnothing (u) ? x : map (v -> uconvert (u, v) , x)
78
+ _withoutunit (u , x)
84
79
end
85
80
86
81
𝒯 = (; zip (names, columns)... )
87
82
𝒯 |> Tables. materializer (newfeat)
88
83
end
84
+
85
+ _withunit (u, x) = _withunit (nonmissingtype (eltype (x)), u, x)
86
+ _withunit (:: Type{Q} , u, x) where {Q<: AbstractQuantity } = (map (v -> uconvert (u, v), x), unit (Q))
87
+ _withunit (:: Type{Q} , u, x) where {Q<: Number } = (x * u, NoUnits)
88
+
89
+ function _withoutunit (u, x)
90
+ if u === NoUnits
91
+ map (ustrip, x)
92
+ else
93
+ map (xᵢ -> uconvert (u, xᵢ), x)
94
+ end
95
+ end
96
+ _withoutunit (:: Nothing , x) = x
0 commit comments