You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I'm curious about the design decision on why the type of .keys is a Tuple when the array is ≥2 dimensional, but a Ref when the array is 1 dimensional?
This prevents me from using X.keys[1] consistently to grab the labels of the first dimension of an array.
Example:
julia> a = KeyedArray(1:10, (1:10,));
julia> b = KeyedArray(reshape(1:10, 5, 2), (1:5, [:A, :B]));
julia> typeof(a.keys)
Base.RefValue{UnitRange{Int64}}
julia> typeof(b.keys)
Tuple{UnitRange{Int64}, Vector{Symbol}}
julia> b.keys[1]
1:5
julia> a.keys[1]
ERROR: MethodError: no method matching getindex(::Base.RefValue{UnitRange{Int64}}, ::Int64)
The function `getindex` exists, but no method is defined for this combination of argument types.
Closest candidates are:
getindex(::Base.RefValue)
@ Base refvalue.jl:59
getindex(::Ref, ::CartesianIndex{0})
@ Base multidimensional.jl:1952
The text was updated successfully, but these errors were encountered:
The "official" way to access these is the function axiskeys, which works like axes:
julia>axiskeys(a) # all, as a tuple
(1:10,)
julia>axiskeys(b)
(1:5, [:A, :B])
julia>axiskeys(a, 1) # just one1:10
julia>axiskeys(b, 1)
1:5
The reason for the Ref is this: the length of a Vector can change, but a Matrix cannot. When you push! to a 1D KeyedArray, whose key-vector is a range, it can mutate the parent Vector, but cannot mutate the range. So instead, it replaces it with an extended version:
This is a great explanation, it taught me both about axiskeys as well as the ability to push! and have the labels be extended. Thanks for your patience and your work on this package!
Hi! I'm curious about the design decision on why the type of
.keys
is aTuple
when the array is ≥2 dimensional, but aRef
when the array is 1 dimensional?This prevents me from using
X.keys[1]
consistently to grab the labels of the first dimension of an array.Example:
The text was updated successfully, but these errors were encountered: