Skip to content

Commit 8b54281

Browse files
authored
Merge pull request #115 from invenia/dimensional-bounds-error
Add and test for a better boundserror when performing implicit value indexing
2 parents 6be0ff6 + e8512bb commit 8b54281

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/indexing.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::Real) =
186186
function axisindexes(::Type{Dimensional}, ax::AbstractVector, idx)
187187
idxs = searchsorted(ax, ClosedInterval(idx,idx))
188188
length(idxs) > 1 && error("more than one datapoint lies on axis value $idx; use an interval to return all values")
189-
idxs[1]
189+
if length(idxs) == 1
190+
idxs[1]
191+
else
192+
throw(BoundsError(ax, idx))
193+
end
190194
end
191195
# Dimensional axes may always be indexed by value if in a Value type wrapper.
192196
function axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::TolValue)

test/indexing.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ axy = @inferred(A[Axis{:y}])
180180
@test axy.val == 1:5
181181
@test_throws ArgumentError A[Axis{:z}]
182182

183+
# indexing by value (implicitly) in a dimensional axis
184+
some_dates = DateTime(2016, 1, 2, 0):Hour(1):DateTime(2016, 1, 2, 2)
185+
A1 = AxisArray(reshape(1:6, 2, 3), Axis{:x}(1:2), Axis{:y}(some_dates))
186+
A2 = AxisArray(reshape(1:6, 2, 3), Axis{:x}(1:2), Axis{:y}(collect(some_dates)))
187+
for A in (A1, A2)
188+
@test A[:, DateTime(2016, 1, 2, 1)] == [3; 4]
189+
@test A[:, DateTime(2016, 1, 2, 1) .. DateTime(2016, 1, 2, 2)] == [3 5; 4 6]
190+
@test_throws BoundsError A[:, DateTime(2016, 1, 2, 3)]
191+
@test_throws BoundsError A[:, DateTime(2016, 1, 1, 23)]
192+
try
193+
A[:, DateTime(2016, 1, 2, 3)]
194+
@test "unreachable" === false
195+
catch err
196+
@test err == BoundsError(A.axes[2].val, DateTime(2016, 1, 2, 3))
197+
end
198+
end
199+
183200
# Test for the expected exception type given repeated axes
184201
A = AxisArray(rand(2,2), :x, :y)
185202
@test_throws ArgumentError A[Axis{:x}(1), Axis{:x}(1)]

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AxisArrays
2+
using Base.Dates
23
using Base.Test
34
import IterTools
45

0 commit comments

Comments
 (0)