Skip to content

Commit

Permalink
add support for Dates
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman committed Jun 11, 2017
1 parent 5c653c1 commit 925958b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ axisindexes{T}(::Type{Dimensional}, ax::AbstractVector{T}, idx::ClosedInterval)
# (the fencepost problem). As such, we need to be careful about how and when we
# snap the provided intervals and offsets to exact axis values (and indices).
#
#
# Note that this is fundamentally different than indexing by a single interval;
# whereas those intervals are specified in the same units as the elements of the
# axis itself, repeated intervals are specified in terms of _offsets_. This is
# most obvious with dates; single intervals are between dates, repeated
# intervals use intervals of days (for example) and offsets of dates.
axisindexes(::Type{Dimensional}, ax::AbstractVector, idx::RepeatedInterval) = error("repeated intervals might select a varying number of elements for non-range axes; use a repeated Range of indices instead")
function axisindexes(::Type{Dimensional}, ax::Range, idx::RepeatedInterval)
idxs, vals = relativewindow(ax, idx.window)
Expand Down
8 changes: 6 additions & 2 deletions src/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ function unsafe_searchsortednearest(vec::Range, x)
end


nsteps(x, step) = floor(Int, abs(x / step)) * Int(sign(x))
function nsteps(x, step)
offset = floor(Int, abs(x / step))
return x < zero(x) ? -offset : offset
end
function nsteps{T}(x, step::Base.TwicePrecision{T})
# this is basically a hack because Base hasn't defined x/step at TwicePrecision resolution
nf = abs(x / convert(T, step))
nc = ceil(Int, nf)
return (abs(convert(T, nc*step)) <= abs(x) ? nc : floor(Int, nf)) * Int(sign(x))
offset = (abs(convert(T, nc*step)) <= abs(x) ? nc : floor(Int, nf))
return x < zero(x) ? -offset : offset
end

_step(r::Range) = step(r)
Expand Down
9 changes: 8 additions & 1 deletion test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ Base.div(x::IntLike, y::IntLike) = div(x.val, y.val)
Base.:*(x::IntLike, y::Int) = IntLike(x.val * y)
Base.:*(x::Int, y::IntLike) = y*x
Base.:/(x::IntLike, y::Int) = IntLike(x.val / y)
Base.abs(x::IntLike) = IntLike(abs(x.val))
Base.promote_rule(::Type{IntLike}, ::Type{Int}) = Int
Base.convert(::Type{Int}, x::IntLike) = x.val
using AxisArrays
Expand Down Expand Up @@ -180,3 +179,11 @@ A = AxisArray(rand(2,2), :x, :y)
acc = zeros(Int, 4, 1, 2)
Base.mapreducedim!(x->x>5, +, acc, A3)
@test acc == reshape([1 3; 2 3; 2 3; 2 3], 4, 1, 2)

# Test using dates
using Base.Dates: Day, Month
A = AxisArray(1:365, Date(2017,1,1):Date(2017,12,31))
@test A[Date(2017,2,1) .. Date(2017,2,28)] == collect(31 + (1:28)) # February
@test A[(-Day(13)..Day(14)) + Date(2017,2,14)] == collect(31 + (1:28))
@test A[(-Day(14)..Day(14)) + DateTime(2017,2,14,12)] == collect(31 + (1:28))
@test A[(Day(0)..Day(6)) + (Date(2017,1,1):Month(1):Date(2017,4,12))] == [1:7 32:38 60:66 91:97]

0 comments on commit 925958b

Please sign in to comment.