@@ -149,45 +149,70 @@ function populate!(A, table, value::Symbol; force=false)
149149end
150150
151151"""
152- wrapdims(table, value, keys...; default=undef, sort=false, force=false) -> KeyedArray
153- wrapdims(T, table, value, keys...; default=undef, sort=false, force=false) -> T
154-
155- Construct a `KeyedArray`/`NamedDimsArray` (specified by type `T`) from a `table` matching
156- the [Tables.jl](https://github.com/JuliaData/Tables.jl) API. The `table` should support both
157- `Tables.columns` and `Tables.rows`. The `default` value is used in cases where no
158- value is identified for a given keypair. If the `keys` columns do not uniquely identify
159- rows in the table then an `ArgumentError` is throw. If `force` is true then the duplicate
160- (non-unique) entries will be overwritten.
152+ wrapdims(table, value, names...; default=undef, sort=false, force=false)
153+
154+ Construct `KeyedArray(NamedDimsArray(A,names),keys)` from a `table` matching
155+ the [Tables.jl](https://github.com/JuliaData/Tables.jl) API.
156+ (It must support both `Tables.columns` and `Tables.rows`.)
157+
158+ The contents of the array is taken from the column `value::Symbol` of the table.
159+ Each symbol in `names` specifies a column whose unique entries
160+ become the keys along a dimenension of the array.
161+
162+ If there is no row in the table matching a possible set of keys,
163+ then this element of the array is undefined, unless you provide the `default` keyword.
164+ If several rows share the same set of keys, then by default an `ArgumentError` is thrown.
165+ Keyword `force=true` will instead cause these non-unique entries to be overwritten.
166+
167+ Setting `AxisKeys.nameouter() = false` will reverse the order of wrappers produced.
168+ """
169+ function wrapdims (table, value:: Symbol , names:: Symbol... ; kw... )
170+ if nameouter () == false
171+ _wrap_table (KeyedArray, identity, table, value, names... ; kw... )
172+ else
173+ _wrap_table (NamedDimsArray, identity, table, value, names... ; kw... )
174+ end
175+ end
176+
177+ """
178+ wrapdims(df, UniqueVector, :val, :x, :y)
179+
180+ Converts at Tables.jl table to a `KeyedArray` + `NamedDimsArray` pair,
181+ using column `:val` for values, and columns `:x, :y` for names & keys.
182+ Optional 2nd argument applies this type to all the key-vectors.
161183"""
162- function wrapdims (table, value:: Symbol , keys:: Symbol... ; kwargs... )
163- wrapdims (KeyedArray, table, value, keys... ; kwargs... )
184+ function wrapdims (table, KT:: Type , value:: Symbol , names:: Symbol... ; kw... )
185+ if nameouter () == false
186+ _wrap_table (KeyedArray, KT, table, value, names... ; kw... )
187+ else
188+ _wrap_table (NamedDimsArray, KT, table, value, names... ; kw... )
189+ end
164190end
165191
166- function wrapdims (T :: Type , table, value:: Symbol , keys :: Symbol... ; default= undef, sort:: Bool = false , kwargs... )
192+ function _wrap_table (AT :: Type , KT, table, value:: Symbol , names :: Symbol... ; default= undef, sort:: Bool = false , kwargs... )
167193 # get columns of the input table source
168194 cols = Tables. columns (table)
169195
170196 # Extract key columns
171- pairs = map (keys ) do k
197+ pairs = map (names ) do k
172198 col = unique (Tables. getcolumn (cols, k))
173199 sort && Base. sort! (col)
174- return k => col
200+ return k => KT ( col)
175201 end
176202
177203 # Extract data/value column
178204 vals = Tables. getcolumn (cols, value)
179205
180206 # Initialize the KeyedArray
181207 sz = length .(last .(pairs))
182-
183- A = if default === undef
208+ if default === undef
184209 data = similar (vals, sz)
185210 else
186211 data = similar (vals, Union{eltype (vals), typeof (default)}, sz)
187212 fill! (data, default)
188213 end
214+ A = AT (data; pairs... )
189215
190- A = T (data; pairs... )
191216 populate! (A, table, value; kwargs... )
192217 return A
193218end
0 commit comments