Skip to content

Commit 4019113

Browse files
committed
Use categorical indexing behaviour by default for unknown array eltypes
1 parent 6be0ff6 commit 4019113

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/core.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ axistrait(::Type{Axis{name,T}}) where {name,T} = axistrait(T)
539539
axistrait(::Type{T}) where {T<:AbstractVector} = _axistrait_el(eltype(T))
540540
_axistrait_el(::Type{<:Union{Number, Dates.AbstractTime}}) = Dimensional
541541
_axistrait_el(::Type{<:Union{Symbol, AbstractString}}) = Categorical
542-
_axistrait_el(::Type{T}) where {T} = Unsupported
542+
_axistrait_el(::Type{T}) where {T} = Categorical
543543

544544
checkaxis(ax::Axis) = checkaxis(ax.val)
545545
checkaxis(ax) = checkaxis(axistrait(ax), ax)

src/indexing.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Base.start(::Value) = false
2626
Base.next(x::Value, state) = (x, true)
2727
Base.done(x::Value, state) = state
2828

29+
# Values have the indexing trait of their wrapped type
30+
_axistrait_el(::Type{<:Value{T}}) where {T} = _axistrait_el(T)
31+
2932
# How to show Value objects (e.g. in a BoundsError)
3033
Base.show(io::IO, v::TolValue) =
3134
print(io, string("TolValue(", v.val, ", tol=", v.tol, ")"))

test/indexing.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ A = AxisArray([1 2; 3 4], Axis{:x}([:a, :b]), Axis{:y}(["c", "d"]))
242242
@test @inferred(A[Axis{:x}(atvalue(:b))]) == [3,4]
243243
@test @inferred(A[Axis{:y}(atvalue("d"))]) == [2,4]
244244

245+
# Index by mystery types categorically
246+
struct Foo
247+
x
248+
end
249+
A = AxisArray(1:10, Axis{:x}(map(Foo, 1:10)))
250+
@test A[map(Foo, 3:6)] == collect(3:6)
251+
@test_throws ArgumentError A[map(Foo, 3:11)]
252+
@test A[Foo(4)] == 4
253+
@test_throws ArgumentError A[Foo(0)]
254+
245255
# Test using dates
246256
using Base.Dates: Day, Month
247257
A = AxisArray(1:365, Date(2017,1,1):Date(2017,12,31))

0 commit comments

Comments
 (0)