Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Lookups/selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,12 @@ function contains(l::NoLookup, sel::Contains; err=_True(), kw...)
end
end
function contains(l::Lookup, sel::Contains; kw...)
val(sel) isa AbstractFloat && isnan(val(sel)) && throw(ArgumentError("NaN not allowed in `Contains`"))
v = val(sel)
v isa AbstractFloat && isnan(v) && throw(ArgumentError("NaN not allowed in `Contains`"))
# Allow Date and DateTime to be used interchangeably
if v isa Union{Dates.DateTime,Dates.Date}
sel = Contains(eltype(l)(v))
end
contains(sampling(l), l, sel; kw...)
end
# NoSampling (e.g. Categorical) just uses `at`
Expand Down Expand Up @@ -453,6 +458,17 @@ function contains(
i = _searchfunc(locus, o)(l, v)
return check_regular_contains(span, locus, l, v, i, err)
end
function contains(
o::Ordered, span::Regular, ::Intervals, locus::Center, l::Lookup, sel::Contains{<:Dates.AbstractTime};
err=_True()
)
v = val(sel)
i = _searchfunc(locus, o)(l, v)
if i > firstindex(l)
i = abs(l[i] - v) < abs(l[i-1] - v) ? i : i - i
end
return check_regular_contains(span, locus, l, v, i, err)
end

function check_regular_contains(span::Span, locus::Locus, l::Lookup, v, i, err)
absstep = abs(val(span))
Expand Down
23 changes: 22 additions & 1 deletion test/selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1187,13 +1187,33 @@ end
@test @inferred da[Ti(At(Date(2001, 3)))] == 3
@test @inferred da[Near(DateTime(2001, 4, 7))] == 4
@test @inferred da[Near(Date(2001, 4, 7))] == 4
@test @inferred da[Near(Date(2001, 4, 7))] == 4
@test @inferred da[DateTime(2001, 4, 7) .. DateTime(2001, 8, 30)] == [5, 6, 7]
@test @inferred da[Date(2001, 4, 7) .. Date(2001, 8, 30)] == [5, 6, 7]

@test_throws SelectorError da[Ti(At(Date(2001, 3, 4); atol=Day(2)))]
@test @inferred da[Ti(At(Date(2001, 3, 4); atol=Day(3)))] == 3
@test @inferred da[Ti(At(DateTime(2001, 3, 4); atol=Day(3)))] == 3
end
@testset "Center locus" begin
timedim = Ti(Sampled(DateTime(2001):Month(1):DateTime(2001, 12);
span=Regular(Month(1)), sampling=Intervals(Center())
))
da = DimArray(1:12, timedim)
@test @inferred da[Ti(At(DateTime(2001, 3)))] == 3
@test @inferred da[Ti(At(Date(2001, 3)))] == 3
@test @inferred da[Near(DateTime(2001, 4, 7))] == 4
@test @inferred da[Near(Date(2001, 4, 7))] == 4
@test @inferred da[Contains(Date(2001, 4, 15))] == 4
@test @inferred da[Contains(DateTime(2001, 4, 15))] == 4
@test @inferred da[Contains(Date(2001, 3, 16))] == 4
@test @inferred da[Contains(DateTime(2001, 4, 15))] == 4
@test @inferred
da[DateTime(2001, 4, 7) .. DateTime(2001, 8, 30)] == [5, 6, 7]
@test_throws SelectorError da[Ti(At(Date(2001, 3, 4); atol=Day(2)))]
@test @inferred da[Ti(At(Date(2001, 3, 4); atol=Day(3)))] == 3
@test @inferred da[Ti(At(DateTime(2001, 3, 4); atol=Day(3)))] == 3
end
@testset "End locus" begin
timedim = Ti(Sampled(DateTime(2001):Month(1):DateTime(2001, 12);
span=Regular(Month(1)), sampling=Intervals(End()))
Expand All @@ -1203,6 +1223,7 @@ end
@test @inferred da[Ti(At(Date(2001, 3)))] == 3
@test @inferred da[Near(DateTime(2001, 4, 7))] == 5
@test @inferred da[Near(Date(2001, 4, 7))] == 5
@test @inferred da[Contains(Date(2001, 4, 7))] == 5
@test @inferred da[DateTime(2001, 4, 7) .. DateTime(2001, 8, 30)] == [6, 7, 8]
@test @inferred da[Date(2001, 4, 7) .. Date(2001, 8, 30)] == [6, 7, 8]

Expand Down Expand Up @@ -1479,4 +1500,4 @@ end
A[Y=At(yval; atol=0.001), X=Near(xval)] ==
A[Y=Near(yval), X=At(xval; atol=0.001)] ==
A[end-10]
end
end
Loading