Skip to content

Commit c99478d

Browse files
committed
Address Milan's comments/suggestions.
1 parent 4e60780 commit c99478d

File tree

1 file changed

+25
-32
lines changed

1 file changed

+25
-32
lines changed

src/tables.jl

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,60 +117,53 @@ end
117117
# end
118118

119119
"""
120-
AxisKeys.populate!(A, table, value; force=false, quiet=false)
120+
AxisKeys.populate!(A, table, value; force=false)
121121
122-
Populate `A` with the contents of the `value` column in a provided `table`.
123-
The `table` must contain columns corresponding to the keys in `A` and support row iteration.
124-
If the keys in `A` do not uniquely identify rows in the `table` then an
125-
`ArgumentError` is throw. If `force` is true then the duplicate (non-unique) entries will be
126-
overwritten. Setting `quiet` to true will avoid any non-unique warnings.
122+
Populate `A` with the contents of the `value` column in a provided `table`, matching the
123+
[Tables.jl](https://github.com/JuliaData/Tables.jl) API. The `table` must contain columns
124+
corresponding to the keys in `A` and implements `Tables.rows`. If the keys in `A` do not
125+
uniquely identify rows in the `table` then an `ArgumentError` is throw. If `force` is true
126+
then the duplicate (non-unique) entries will be overwritten.
127127
"""
128-
function populate!(A, table, value::Symbol; force=false, quiet=false)
128+
function populate!(A, table, value::Symbol; force=false)
129129
# Use a BitArray mask to detect duplicates and error instead of overwriting.
130-
mask = falses(size(A))
131-
overwritten = false
130+
mask = force ? falses() : falses(size(A))
132131

133132
for r in Tables.rows(table)
134133
vals = Tuple(Tables.getcolumn(r, c) for c in dimnames(A))
135134
inds = map(findindex, vals, axiskeys(A))
136135

137-
# Handle duplicate error if applicable
138-
if mask[inds...]
139-
if force
140-
overwritten = true
141-
else
142-
throw(ArgumentError("Key $vals is not unique"))
143-
end
136+
# Handle duplicate error checking if applicable
137+
if !force
138+
# Error if mask already set.
139+
mask[inds...] && throw(ArgumentError("Key $vals is not unique"))
140+
# Set mask, marking that we've set this index
141+
setindex!(mask, true, inds...)
144142
end
145143

146-
# Set or mask marking that we've set this index
147-
setindex!(mask, true, inds...)
148-
149144
# Insert our value into the data array
150145
setindex!(A, Tables.getcolumn(r, value), inds...)
151146
end
152147

153-
if overwritten && !quiet
154-
@warn "Columns $(dimnames(A)) do not uniquely identify rows in the table"
155-
end
156-
157148
return A
158149
end
159150

160151
"""
161-
wrapdims(table, [Type,] value, keys...; default=undef, sort=false, force=false, quiet=false)
162-
163-
Construct a KeyedArray/NamedDimsArray from a `table` supporting column and row.
164-
The `default` value is used in cases where no value is identified for a given keypair.
165-
If the `keys` columns do not uniquely identify rows in the table then an `ArgumentError` is
166-
throw. If `force` is true then the duplicate (non-unique) entries will be
167-
overwritten. Setting `quiet` to true will avoid any non-unique warnings.
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.
168161
"""
169162
function wrapdims(table, value::Symbol, keys::Symbol...; kwargs...)
170-
wrapdims(table, KeyedArray, value, keys...; kwargs...)
163+
wrapdims(KeyedArray, table, value, keys...; kwargs...)
171164
end
172165

173-
function wrapdims(table, T::Type, value::Symbol, keys::Symbol...; default=undef, sort::Bool=false, kwargs...)
166+
function wrapdims(T::Type, table, value::Symbol, keys::Symbol...; default=undef, sort::Bool=false, kwargs...)
174167
# get columns of the input table source
175168
cols = Tables.columns(table)
176169

0 commit comments

Comments
 (0)