Skip to content

Commit 4062e26

Browse files
committed
add support for Dates
1 parent 5c653c1 commit 4062e26

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

src/core.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,3 +558,11 @@ _size(A::AbstractArray) = map(length, indices(A))
558558
_size(A) = size(A)
559559
_size(A::AbstractArray, d) = length(indices(A, d))
560560
_size(A, d) = size(A, d)
561+
562+
Base.flipdim(A::AxisArray, d::Integer) = flipdim(A, A.axes[d])
563+
Base.flipdim(A::AxisArray, ax::Axis) = flipdim(A, typeof(ax))
564+
Base.flipdim{T<:Axis}(A::AxisArray, ::Type{T}) = AxisArray(flipdim(A.data, axisdim(A, T)), flipax(A.axes, T)...)
565+
flipax(::Tuple{}, ::Type) = ()
566+
flipax{T,X,Y}(t::Tuple{Axis{T,X}, Vararg{Any}}, ::Type{Axis{T,Y}}) = (Axis{T}(flipdim(t[1].val, 1)), tail(t)...)
567+
flipax{S,X}(t::Tuple{Axis{S,X}, Vararg{Any}}, ::Type{Axis{S}}) = (Axis{T}(flipdim(t[1].val, 1)), tail(t)...)
568+
flipax{T}(t::Tuple, ::Type{T}) = (t[1], flipax(tail(t), T)...)

src/indexing.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,11 @@ axisindexes{T}(::Type{Dimensional}, ax::AbstractVector{T}, idx::ClosedInterval)
199199
# (the fencepost problem). As such, we need to be careful about how and when we
200200
# snap the provided intervals and offsets to exact axis values (and indices).
201201
#
202-
#
202+
# Note that this is fundamentally different than indexing by a single interval;
203+
# whereas those intervals are specified in the same units as the elements of the
204+
# axis itself, repeated intervals are specified in terms of _offsets_. This is
205+
# most obvious with dates; single intervals are between dates, repeated
206+
# intervals use intervals of days (for example) and offsets of dates.
203207
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")
204208
function axisindexes(::Type{Dimensional}, ax::Range, idx::RepeatedInterval)
205209
idxs, vals = relativewindow(ax, idx.window)

src/search.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ function unsafe_searchsortednearest(vec::Range, x)
2525
end
2626

2727

28-
nsteps(x, step) = floor(Int, abs(x / step)) * Int(sign(x))
28+
function nsteps(x, step)
29+
offset = floor(Int, abs(x / step))
30+
return x < zero(x) ? -offset : offset
31+
end
2932
function nsteps{T}(x, step::Base.TwicePrecision{T})
3033
# this is basically a hack because Base hasn't defined x/step at TwicePrecision resolution
3134
nf = abs(x / convert(T, step))
3235
nc = ceil(Int, nf)
33-
return (abs(convert(T, nc*step)) <= abs(x) ? nc : floor(Int, nf)) * Int(sign(x))
36+
offset = (abs(convert(T, nc*step)) <= abs(x) ? nc : floor(Int, nf))
37+
return x < zero(x) ? -offset : offset
3438
end
3539

3640
_step(r::Range) = step(r)

test/indexing.jl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ Base.div(x::IntLike, y::IntLike) = div(x.val, y.val)
121121
Base.:*(x::IntLike, y::Int) = IntLike(x.val * y)
122122
Base.:*(x::Int, y::IntLike) = y*x
123123
Base.:/(x::IntLike, y::Int) = IntLike(x.val / y)
124-
Base.abs(x::IntLike) = IntLike(abs(x.val))
125124
Base.promote_rule(::Type{IntLike}, ::Type{Int}) = Int
126125
Base.convert(::Type{Int}, x::IntLike) = x.val
127126
using AxisArrays
@@ -180,3 +179,17 @@ A = AxisArray(rand(2,2), :x, :y)
180179
acc = zeros(Int, 4, 1, 2)
181180
Base.mapreducedim!(x->x>5, +, acc, A3)
182181
@test acc == reshape([1 3; 2 3; 2 3; 2 3], 4, 1, 2)
182+
183+
# Test using dates
184+
using Base.Dates: Day, Month
185+
A = AxisArray(1:365, Date(2017,1,1):Date(2017,12,31))
186+
@test A[Date(2017,2,1) .. Date(2017,2,28)] == collect(31 + (1:28)) # February
187+
@test A[(-Day(13)..Day(14)) + Date(2017,2,14)] == collect(31 + (1:28))
188+
@test A[(-Day(14)..Day(14)) + DateTime(2017,2,14,12)] == collect(31 + (1:28))
189+
@test A[(Day(0)..Day(6)) + (Date(2017,1,1):Month(1):Date(2017,4,12))] == [1:7 32:38 60:66 91:97]
190+
191+
B = flipdim(A, 1)
192+
@test B[Date(2017,2,1) .. Date(2017,2,28)] == collect(31 + (1:28)) # February
193+
@test B[(-Day(13)..Day(14)) + Date(2017,2,14)] == collect(31 + (1:28))
194+
@test B[(-Day(14)..Day(14)) + DateTime(2017,2,14,12)] == collect(31 + (1:28))
195+
@test B[(Day(0)..Day(6)) + (Date(2017,1,1):Month(1):Date(2017,4,12))] == [1:7 32:38 60:66 91:97]

0 commit comments

Comments
 (0)