AxisKeys.jl icon indicating copy to clipboard operation
AxisKeys.jl copied to clipboard

Selecting fails with vector

Open aplavin opened this issue 3 years ago • 3 comments

Somehow, behavior for key-based selections differs between ranges and vectors. Selecting with vectors fails when some of the keys are not found:

julia> ka = KeyedArray(rand(3), x=10:12)

julia> ka(x=12:13)
1-dimensional KeyedArray(NamedDimsArray(...)) with keys:
↓   x ∈ 1-element UnitRange{Int64}
And data, 1-element view(::Vector{Float64}, 3:3) with eltype Float64:
 (12)  0.8790116535631433

julia> ka(x=[12, 13])
ERROR: ArgumentError: unable to check bounds for indices of type Nothing

aplavin avatar Mar 09 '22 16:03 aplavin

Probably can be fixed by replacing https://github.com/mcabbott/AxisKeys.jl/blob/a78ba3d0ecc47d9345aeed4e773bf44056719d32/src/lookup.jl#L87 with findall(∈(Set(a)), r), but I'm not completely sure.

aplavin avatar Mar 09 '22 16:03 aplavin

I think those should both be errors. But apparently little effort was made to check & handle them. This is an error but perhaps it should be a more helpful error:

julia> ka(x=13)
ERROR: ArgumentError: invalid index: nothing of type Nothing

mcabbott avatar Mar 09 '22 16:03 mcabbott

Interesting, I thought the intended behavior with collections (ranges/vectors) is to keep matches, drop non-matches, and don't throw anything.

aplavin avatar Mar 09 '22 16:03 aplavin