From 341ce72eb44dde916181398dce63950ef3d59c99 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 10:47:33 -0400 Subject: [PATCH 01/15] Lead/lag with respect to a time variable --- Project.toml | 3 ++- src/lag.jl | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 16 ++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 72d3119..79b9c0b 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,7 @@ julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [targets] -test = ["Test"] +test = ["Test", "Dates"] diff --git a/src/lag.jl b/src/lag.jl index e0c0962..68e1f1c 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -97,3 +97,61 @@ julia> s = lead(v, (0, 2)) ``` """ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); default = default) + + + + + + + + +""" + lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) -> Vector + +Shifts with respect to a time variable `dt`. The third variable refers to the amount of time to shift. +`default` specifies a default value when the shifted period is not in the time variable. +Elements in the time variable `dt` must all be distinct. + +# Examples + +```jldoctest lead +julia> v = [1, 3, 5, 4]; +julia> dt = [1990, 1992, 1993]; +julia> lag(v, dt, 1) +3-element Array{Union{Missing, Int64},1}: + missing + missing + 3 +""" +function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)), default = missing) + inds = keys(dt) + dtdict = Dict{eltype(dt),eltype(inds)}() + for (val, ind) in zip(dt, inds) + out = get!(dtdict, val, ind) + out != ind && error("Elements of dt must be distinct") + end + Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] +end + +""" + lead(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) -> Vector + +Shifts with respect to a time variable `dt`. The third variable refers to the amount of time to shift. +`default` specifies a default value when the shifted period is not in the time variable. +Elements in the time variable `dt` must all be distinct. + +# Examples + +```jldoctest lead +julia> v = [1, 3, 5, 4]; +julia> dt = [1990, 1992, 1993]; +julia> lead(v, dt, 1) +3-element Array{Union{Missing, Int64},1}: + missing + 5 + missing +""" +function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)), default = missing) + lag(v, dt, -period, default) +end + diff --git a/test/runtests.jl b/test/runtests.jl index 65370b4..bd9cc34 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using ShiftedArrays, Test +using ShiftedArrays, Test, Dates @testset "ShiftedVector" begin v = [1, 3, 5, 4] @@ -96,4 +96,18 @@ end @test isequal(diff2, [-7, -9, missing, missing]) @test all(lead(v, 2, default = -100) .== coalesce.(lead(v, 2), -100)) + + + x = [4, 5, 6] + date = [1989, 1991, 1992] + @test all(lag(x, date) .=== [missing, missing, 5]) + date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] + @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) + @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) + @test all(lag(x, date, Day(5)) .=== [missing, missing, missing]) + @test all(lead(x, date, Day(1)) .=== [missing, 6, missing]) + @test all(lead(x, date, Day(2)) .=== [5, missing, missing]) + @test all(lead(x, date, Day(5)) .=== [missing, missing, missing]) + date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] + @test_throws ErrorException lag(x, date, Day(1)) end From 10ab355533256fd344cb0c9142eec36883ae0f49 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 12:01:08 -0400 Subject: [PATCH 02/15] kwarg --- src/lag.jl | 6 +++--- test/runtests.jl | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index 68e1f1c..569a82f 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -123,7 +123,7 @@ julia> lag(v, dt, 1) missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)), default = missing) +function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) inds = keys(dt) dtdict = Dict{eltype(dt),eltype(inds)}() for (val, ind) in zip(dt, inds) @@ -151,7 +151,7 @@ julia> lead(v, dt, 1) 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)), default = missing) - lag(v, dt, -period, default) +function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) + lag(v, dt, -period; default = default) end diff --git a/test/runtests.jl b/test/runtests.jl index bd9cc34..4ef17b6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -101,6 +101,8 @@ end x = [4, 5, 6] date = [1989, 1991, 1992] @test all(lag(x, date) .=== [missing, missing, 5]) + @test all(lag(x, date; default = 0) .=== [0, 0, 5]) + date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) From a011898de2d966b13ef55e48b0764080378b8584 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 12:03:43 -0400 Subject: [PATCH 03/15] Update lag.jl --- src/lag.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index 569a82f..2294f6a 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,10 +106,10 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) -> Vector + lag(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) -> Vector -Shifts with respect to a time variable `dt`. The third variable refers to the amount of time to shift. -`default` specifies a default value when the shifted period is not in the time variable. +Shifts with respect to a time variable `dt`. The third variable refers to the period to shift. +`default` specifies a default value when the shifted time is not in the time variable. Elements in the time variable `dt` must all be distinct. # Examples @@ -123,21 +123,21 @@ julia> lag(v, dt, 1) missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) +function lag(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) inds = keys(dt) dtdict = Dict{eltype(dt),eltype(inds)}() for (val, ind) in zip(dt, inds) out = get!(dtdict, val, ind) out != ind && error("Elements of dt must be distinct") end - Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] + Union{eltype(v), typeof(default)}[(i = get(dtdict, x - n, nothing); i !== nothing ? v[i] : default) for x in dt] end """ - lead(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) -> Vector + lead(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) -> Vector -Shifts with respect to a time variable `dt`. The third variable refers to the amount of time to shift. -`default` specifies a default value when the shifted period is not in the time variable. +Shifts with respect to a time variable `dt`. The third variable refers to the period to shift. +`default` specifies a default value when the shifted to,e is not in the time variable. Elements in the time variable `dt` must all be distinct. # Examples @@ -151,7 +151,7 @@ julia> lead(v, dt, 1) 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) - lag(v, dt, -period; default = default) +function lead(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) + lag(v, dt, -n; default = default) end From ddfc16a9540d1d98fa3f5d4290b2d893c072f4da Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 12:29:27 -0400 Subject: [PATCH 04/15] add default for TimeType --- Project.toml | 4 ++-- src/lag.jl | 18 ++++++++++++++---- test/runtests.jl | 5 ++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index 79b9c0b..625a65f 100644 --- a/Project.toml +++ b/Project.toml @@ -4,13 +4,13 @@ repo = "https://github.com/JuliaArrays/ShiftedArrays.jl.git" version = "1.0.0" [deps] +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [compat] julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [targets] -test = ["Test", "Dates"] +test = ["Test"] diff --git a/src/lag.jl b/src/lag.jl index 2294f6a..964c785 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,7 +106,7 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) -> Vector + lag(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) -> Vector Shifts with respect to a time variable `dt`. The third variable refers to the period to shift. `default` specifies a default value when the shifted time is not in the time variable. @@ -123,7 +123,7 @@ julia> lag(v, dt, 1) missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) +function lag(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) inds = keys(dt) dtdict = Dict{eltype(dt),eltype(inds)}() for (val, ind) in zip(dt, inds) @@ -134,7 +134,7 @@ function lag(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); def end """ - lead(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) -> Vector + lead(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) -> Vector Shifts with respect to a time variable `dt`. The third variable refers to the period to shift. `default` specifies a default value when the shifted to,e is not in the time variable. @@ -151,7 +151,17 @@ julia> lead(v, dt, 1) 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, n = oneunit(eltype(dt)); default = missing) +function lead(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) lag(v, dt, -n; default = default) end +onestep(::Type{T}) where {T} = oneunit(T) +using Dates +if VERSION >= v"1.5.0-DEV.634" + onestep(::Type{T}) where {T <: TimeType} = eps(T) +else + onestep(::Type{Time}) = Nanosecond(1) + onestep(::Type{DateTime}) = Millisecond(1) + onestep(::Type{Date}) = Day(1) +end + diff --git a/test/runtests.jl b/test/runtests.jl index 4ef17b6..af1d493 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -102,14 +102,17 @@ end date = [1989, 1991, 1992] @test all(lag(x, date) .=== [missing, missing, 5]) @test all(lag(x, date; default = 0) .=== [0, 0, 5]) - date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) + @test all(lag(x, date) .=== [missing, missing, 5]) @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) @test all(lag(x, date, Day(5)) .=== [missing, missing, missing]) @test all(lead(x, date, Day(1)) .=== [missing, 6, missing]) @test all(lead(x, date, Day(2)) .=== [5, missing, missing]) @test all(lead(x, date, Day(5)) .=== [missing, missing, missing]) + date = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] + @test all(lag(x, date) .=== [missing, missing, missing]) + @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] @test_throws ErrorException lag(x, date, Day(1)) end From db52b7ad8d0a39f342a5f149a2e0797cbc3258e5 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 14:00:57 -0400 Subject: [PATCH 05/15] don't import Dates --- Project.toml | 5 +++-- src/lag.jl | 44 ++++++++++++++++++++------------------------ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/Project.toml b/Project.toml index 625a65f..b0b6f64 100644 --- a/Project.toml +++ b/Project.toml @@ -4,13 +4,14 @@ repo = "https://github.com/JuliaArrays/ShiftedArrays.jl.git" version = "1.0.0" [deps] -Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" + [compat] julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [targets] -test = ["Test"] +test = ["Test", "Dates"] diff --git a/src/lag.jl b/src/lag.jl index 964c785..df829b3 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,39 +106,46 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) -> Vector + lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) -> Vector -Shifts with respect to a time variable `dt`. The third variable refers to the period to shift. -`default` specifies a default value when the shifted time is not in the time variable. -Elements in the time variable `dt` must all be distinct. +Shifts with respect to a times given in the vector `dt`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `dt`. +Elements in `dt` must all be distinct. # Examples ```jldoctest lead julia> v = [1, 3, 5, 4]; julia> dt = [1990, 1992, 1993]; -julia> lag(v, dt, 1) +julia> lag(v, dt) 3-element Array{Union{Missing, Int64},1}: missing missing 3 +julia> using Dates +julia> dt = [Date(1990, 1, 1), Date(1990, 1, 3), Date(1990, 1, 4)] +julia> lag(v, dt, Day(1)) +3-element Array{Union{Missing, Int64},1}: + missing + missing +3 """ -function lag(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) +function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) inds = keys(dt) dtdict = Dict{eltype(dt),eltype(inds)}() for (val, ind) in zip(dt, inds) out = get!(dtdict, val, ind) out != ind && error("Elements of dt must be distinct") end - Union{eltype(v), typeof(default)}[(i = get(dtdict, x - n, nothing); i !== nothing ? v[i] : default) for x in dt] + Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] end """ - lead(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) -> Vector + lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) -> Vector -Shifts with respect to a time variable `dt`. The third variable refers to the period to shift. -`default` specifies a default value when the shifted to,e is not in the time variable. -Elements in the time variable `dt` must all be distinct. +Shifts with respect to a vector of times `dt`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `dt`. +Elements in `dt` must all be distinct. # Examples @@ -151,17 +158,6 @@ julia> lead(v, dt, 1) 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, n = onestep(eltype(dt)); default = missing) - lag(v, dt, -n; default = default) -end - -onestep(::Type{T}) where {T} = oneunit(T) -using Dates -if VERSION >= v"1.5.0-DEV.634" - onestep(::Type{T}) where {T <: TimeType} = eps(T) -else - onestep(::Type{Time}) = Nanosecond(1) - onestep(::Type{DateTime}) = Millisecond(1) - onestep(::Type{Date}) = Day(1) +function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) + lag(v, dt, -period; default = default) end - From 725574ebc3eb7b76b3ac20d2f446ae0549f3e5ec Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 14:01:14 -0400 Subject: [PATCH 06/15] up --- test/runtests.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index af1d493..f90d25a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -104,14 +104,13 @@ end @test all(lag(x, date; default = 0) .=== [0, 0, 5]) date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) - @test all(lag(x, date) .=== [missing, missing, 5]) @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) @test all(lag(x, date, Day(5)) .=== [missing, missing, missing]) @test all(lead(x, date, Day(1)) .=== [missing, 6, missing]) @test all(lead(x, date, Day(2)) .=== [5, missing, missing]) @test all(lead(x, date, Day(5)) .=== [missing, missing, missing]) date = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] - @test all(lag(x, date) .=== [missing, missing, missing]) + @test all(lag(x, date, Millisecond(1)) .=== [missing, missing, missing]) @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] @test_throws ErrorException lag(x, date, Day(1)) From 08b8f50eb32ca3ed90433945fdd97eec79649bce Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 16:11:22 -0400 Subject: [PATCH 07/15] add Dates --- Project.toml | 4 ++-- src/lag.jl | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index b0b6f64..afc700b 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ repo = "https://github.com/JuliaArrays/ShiftedArrays.jl.git" version = "1.0.0" [deps] +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [compat] @@ -11,7 +12,6 @@ julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [targets] -test = ["Test", "Dates"] +test = ["Test"] diff --git a/src/lag.jl b/src/lag.jl index df829b3..a6c0a6c 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,7 +106,7 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) -> Vector + lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt))); default = missing) -> Vector Shifts with respect to a times given in the vector `dt`. The third variable `period` gives the period by which to shift. `default` specifies a default value when the shifted time is not in `dt`. @@ -130,7 +130,7 @@ julia> lag(v, dt, Day(1)) missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) +function lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) inds = keys(dt) dtdict = Dict{eltype(dt),eltype(inds)}() for (val, ind) in zip(dt, inds) @@ -139,9 +139,14 @@ function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)) end Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] end +onestep(::Type{T}) where {T} = oneunit(T) +if VERSION >= v"v1.5" + using Dates + onestep(::Type{T}) where {T <: TimeType} = eps(T) +end """ - lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) -> Vector + lead(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt))); default = missing) -> Vector Shifts with respect to a vector of times `dt`. The third variable `period` gives the period by which to shift. `default` specifies a default value when the shifted time is not in `dt`. @@ -158,6 +163,6 @@ julia> lead(v, dt, 1) 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(eltype(dt)); default = missing) +function lead(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) lag(v, dt, -period; default = default) end From f1e480e3261150b744d353d477eb0f5d4f40e101 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Wed, 22 Apr 2020 14:03:24 -0400 Subject: [PATCH 08/15] Update lag.jl --- src/lag.jl | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index a6c0a6c..4842b2d 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,7 +106,7 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt))); default = missing) -> Vector + lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector Shifts with respect to a times given in the vector `dt`. The third variable `period` gives the period by which to shift. `default` specifies a default value when the shifted time is not in `dt`. @@ -130,7 +130,7 @@ julia> lag(v, dt, Day(1)) missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) +function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) inds = keys(dt) dtdict = Dict{eltype(dt),eltype(inds)}() for (val, ind) in zip(dt, inds) @@ -139,14 +139,9 @@ function lag(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)) end Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] end -onestep(::Type{T}) where {T} = oneunit(T) -if VERSION >= v"v1.5" - using Dates - onestep(::Type{T}) where {T <: TimeType} = eps(T) -end """ - lead(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt))); default = missing) -> Vector + lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector Shifts with respect to a vector of times `dt`. The third variable `period` gives the period by which to shift. `default` specifies a default value when the shifted time is not in `dt`. @@ -163,6 +158,6 @@ julia> lead(v, dt, 1) 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, period = onestep(eltype(dt)); default = missing) +function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) lag(v, dt, -period; default = default) end From 41923fdcaec8262f6b6e02f1d4def406985310c8 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Wed, 22 Apr 2020 14:03:52 -0400 Subject: [PATCH 09/15] Update Project.toml --- Project.toml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index afc700b..79b9c0b 100644 --- a/Project.toml +++ b/Project.toml @@ -4,14 +4,13 @@ repo = "https://github.com/JuliaArrays/ShiftedArrays.jl.git" version = "1.0.0" [deps] -Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" - [compat] julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [targets] -test = ["Test"] +test = ["Test", "Dates"] From 8f94ab646bd93fa8aae4dc83e46b6f6567aff9ad Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Thu, 23 Apr 2020 09:52:45 -0400 Subject: [PATCH 10/15] add more tests --- src/lag.jl | 46 +++++++++++++++++++++++----------------------- test/runtests.jl | 35 +++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index 4842b2d..cf8eb0f 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,58 +106,58 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector + lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) -> Vector -Shifts with respect to a times given in the vector `dt`. The third variable `period` gives the period by which to shift. -`default` specifies a default value when the shifted time is not in `dt`. -Elements in `dt` must all be distinct. +Shifts with respect to a times given in the vector `times`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `times`. +Elements in `times` must all be distinct. # Examples ```jldoctest lead julia> v = [1, 3, 5, 4]; -julia> dt = [1990, 1992, 1993]; -julia> lag(v, dt) +julia> times = [1990, 1992, 1993]; +julia> lag(v, times) 3-element Array{Union{Missing, Int64},1}: missing missing 3 julia> using Dates -julia> dt = [Date(1990, 1, 1), Date(1990, 1, 3), Date(1990, 1, 4)] -julia> lag(v, dt, Day(1)) +julia> times = [Date(1990, 1, 1), Date(1990, 1, 3), Date(1990, 1, 4)] +julia> lag(v, times, Day(1)) 3-element Array{Union{Missing, Int64},1}: missing missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) - inds = keys(dt) - dtdict = Dict{eltype(dt),eltype(inds)}() - for (val, ind) in zip(dt, inds) - out = get!(dtdict, val, ind) - out != ind && error("Elements of dt must be distinct") +function lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) + inds = keys(times) + timesdict = Dict{eltype(times),eltype(inds)}() + for (val, ind) in zip(times, inds) + out = get!(timesdict, val, ind) + out != ind && error("Times must be distinct") end - Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] + Union{eltype(v), typeof(default)}[(i = get(timesdict, x - period, nothing); i !== nothing ? v[i] : default) for x in times] end """ - lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector + lead(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) -> Vector -Shifts with respect to a vector of times `dt`. The third variable `period` gives the period by which to shift. -`default` specifies a default value when the shifted time is not in `dt`. -Elements in `dt` must all be distinct. +Shifts with respect to a vector of times `times`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `times`. +Elements in `times` must all be distinct. # Examples ```jldoctest lead julia> v = [1, 3, 5, 4]; -julia> dt = [1990, 1992, 1993]; -julia> lead(v, dt, 1) +julia> times = [1990, 1992, 1993]; +julia> lead(v, times, 1) 3-element Array{Union{Missing, Int64},1}: missing 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) - lag(v, dt, -period; default = default) +function lead(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) + lag(v, times, -period; default = default) end diff --git a/test/runtests.jl b/test/runtests.jl index f90d25a..2c6553b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,20 +98,23 @@ end @test all(lead(v, 2, default = -100) .== coalesce.(lead(v, 2), -100)) - x = [4, 5, 6] - date = [1989, 1991, 1992] - @test all(lag(x, date) .=== [missing, missing, 5]) - @test all(lag(x, date; default = 0) .=== [0, 0, 5]) - date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] - @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) - @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) - @test all(lag(x, date, Day(5)) .=== [missing, missing, missing]) - @test all(lead(x, date, Day(1)) .=== [missing, 6, missing]) - @test all(lead(x, date, Day(2)) .=== [5, missing, missing]) - @test all(lead(x, date, Day(5)) .=== [missing, missing, missing]) - date = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] - @test all(lag(x, date, Millisecond(1)) .=== [missing, missing, missing]) - @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) - date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] - @test_throws ErrorException lag(x, date, Day(1)) + v = [4, 5, 6] + times = [1989, 1991, 1992] + @test all(lag(v, times) .=== [missing, missing, 5]) + @test all(lag(v, times; default = 0) .=== [0, 0, 5]) + @test all(lead(v, times) .=== [missing, 6, missing]) + @test all(lead(v, times; default = 0) .=== [0, 6, 0]) + times = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] + @test all(lag(v, times, Day(1)) .=== [missing, missing, 5]) + @test all(lag(v, times, Day(2)) .=== [missing, 4, missing]) + @test all(lag(v, times, Day(5)) .=== [missing, missing, missing]) + @test all(lead(v, times, Day(1)) .=== [missing, 6, missing]) + @test all(lead(v, times, Day(1)) .=== lag(v, times, -Day(1))) + @test all(lead(v, times, Day(2)) .=== [5, missing, missing]) + @test all(lead(v, times, Day(5)) .=== [missing, missing, missing]) + times = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] + @test all(lag(v, times, Millisecond(1)) .=== [missing, missing, missing]) + @test all(lag(v, times, Day(1)) .=== [missing, missing, 5]) + times = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] + @test_throws ErrorException lag(v, times, Day(1)) end From 07731ddb9dae894fbeb62a399700b1dbbd1e42df Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Tue, 21 Apr 2020 10:47:33 -0400 Subject: [PATCH 11/15] Lead/lag with respect to a time variable --- Project.toml | 3 ++- src/lag.jl | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 20 ++++++++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 72d3119..79b9c0b 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,7 @@ julia = "1" [extras] Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" [targets] -test = ["Test"] +test = ["Test", "Dates"] diff --git a/src/lag.jl b/src/lag.jl index e0c0962..4842b2d 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -97,3 +97,67 @@ julia> s = lead(v, (0, 2)) ``` """ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); default = default) + + + + + + + + +""" + lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector + +Shifts with respect to a times given in the vector `dt`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `dt`. +Elements in `dt` must all be distinct. + +# Examples + +```jldoctest lead +julia> v = [1, 3, 5, 4]; +julia> dt = [1990, 1992, 1993]; +julia> lag(v, dt) +3-element Array{Union{Missing, Int64},1}: + missing + missing + 3 +julia> using Dates +julia> dt = [Date(1990, 1, 1), Date(1990, 1, 3), Date(1990, 1, 4)] +julia> lag(v, dt, Day(1)) +3-element Array{Union{Missing, Int64},1}: + missing + missing +3 +""" +function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) + inds = keys(dt) + dtdict = Dict{eltype(dt),eltype(inds)}() + for (val, ind) in zip(dt, inds) + out = get!(dtdict, val, ind) + out != ind && error("Elements of dt must be distinct") + end + Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] +end + +""" + lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector + +Shifts with respect to a vector of times `dt`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `dt`. +Elements in `dt` must all be distinct. + +# Examples + +```jldoctest lead +julia> v = [1, 3, 5, 4]; +julia> dt = [1990, 1992, 1993]; +julia> lead(v, dt, 1) +3-element Array{Union{Missing, Int64},1}: + missing + 5 + missing +""" +function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) + lag(v, dt, -period; default = default) +end diff --git a/test/runtests.jl b/test/runtests.jl index 65370b4..f90d25a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using ShiftedArrays, Test +using ShiftedArrays, Test, Dates @testset "ShiftedVector" begin v = [1, 3, 5, 4] @@ -96,4 +96,22 @@ end @test isequal(diff2, [-7, -9, missing, missing]) @test all(lead(v, 2, default = -100) .== coalesce.(lead(v, 2), -100)) + + + x = [4, 5, 6] + date = [1989, 1991, 1992] + @test all(lag(x, date) .=== [missing, missing, 5]) + @test all(lag(x, date; default = 0) .=== [0, 0, 5]) + date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] + @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) + @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) + @test all(lag(x, date, Day(5)) .=== [missing, missing, missing]) + @test all(lead(x, date, Day(1)) .=== [missing, 6, missing]) + @test all(lead(x, date, Day(2)) .=== [5, missing, missing]) + @test all(lead(x, date, Day(5)) .=== [missing, missing, missing]) + date = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] + @test all(lag(x, date, Millisecond(1)) .=== [missing, missing, missing]) + @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) + date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] + @test_throws ErrorException lag(x, date, Day(1)) end From b7933c4e33e7819f279ee24b3c59bab6b95548ea Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Thu, 23 Apr 2020 09:52:45 -0400 Subject: [PATCH 12/15] add more tests --- src/lag.jl | 46 +++++++++++++++++++++++----------------------- test/runtests.jl | 35 +++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index 4842b2d..cf8eb0f 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,58 +106,58 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector + lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) -> Vector -Shifts with respect to a times given in the vector `dt`. The third variable `period` gives the period by which to shift. -`default` specifies a default value when the shifted time is not in `dt`. -Elements in `dt` must all be distinct. +Shifts with respect to a times given in the vector `times`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `times`. +Elements in `times` must all be distinct. # Examples ```jldoctest lead julia> v = [1, 3, 5, 4]; -julia> dt = [1990, 1992, 1993]; -julia> lag(v, dt) +julia> times = [1990, 1992, 1993]; +julia> lag(v, times) 3-element Array{Union{Missing, Int64},1}: missing missing 3 julia> using Dates -julia> dt = [Date(1990, 1, 1), Date(1990, 1, 3), Date(1990, 1, 4)] -julia> lag(v, dt, Day(1)) +julia> times = [Date(1990, 1, 1), Date(1990, 1, 3), Date(1990, 1, 4)] +julia> lag(v, times, Day(1)) 3-element Array{Union{Missing, Int64},1}: missing missing 3 """ -function lag(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) - inds = keys(dt) - dtdict = Dict{eltype(dt),eltype(inds)}() - for (val, ind) in zip(dt, inds) - out = get!(dtdict, val, ind) - out != ind && error("Elements of dt must be distinct") +function lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) + inds = keys(times) + timesdict = Dict{eltype(times),eltype(inds)}() + for (val, ind) in zip(times, inds) + out = get!(timesdict, val, ind) + out != ind && error("Times must be distinct") end - Union{eltype(v), typeof(default)}[(i = get(dtdict, x - period, nothing); i !== nothing ? v[i] : default) for x in dt] + Union{eltype(v), typeof(default)}[(i = get(timesdict, x - period, nothing); i !== nothing ? v[i] : default) for x in times] end """ - lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) -> Vector + lead(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) -> Vector -Shifts with respect to a vector of times `dt`. The third variable `period` gives the period by which to shift. -`default` specifies a default value when the shifted time is not in `dt`. -Elements in `dt` must all be distinct. +Shifts with respect to a vector of times `times`. The third variable `period` gives the period by which to shift. +`default` specifies a default value when the shifted time is not in `times`. +Elements in `times` must all be distinct. # Examples ```jldoctest lead julia> v = [1, 3, 5, 4]; -julia> dt = [1990, 1992, 1993]; -julia> lead(v, dt, 1) +julia> times = [1990, 1992, 1993]; +julia> lead(v, times, 1) 3-element Array{Union{Missing, Int64},1}: missing 5 missing """ -function lead(v::AbstractVector, dt::AbstractVector, period = oneunit(zero(eltype(dt))); default = missing) - lag(v, dt, -period; default = default) +function lead(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) + lag(v, times, -period; default = default) end diff --git a/test/runtests.jl b/test/runtests.jl index f90d25a..2c6553b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,20 +98,23 @@ end @test all(lead(v, 2, default = -100) .== coalesce.(lead(v, 2), -100)) - x = [4, 5, 6] - date = [1989, 1991, 1992] - @test all(lag(x, date) .=== [missing, missing, 5]) - @test all(lag(x, date; default = 0) .=== [0, 0, 5]) - date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] - @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) - @test all(lag(x, date, Day(2)) .=== [missing, 4, missing]) - @test all(lag(x, date, Day(5)) .=== [missing, missing, missing]) - @test all(lead(x, date, Day(1)) .=== [missing, 6, missing]) - @test all(lead(x, date, Day(2)) .=== [5, missing, missing]) - @test all(lead(x, date, Day(5)) .=== [missing, missing, missing]) - date = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] - @test all(lag(x, date, Millisecond(1)) .=== [missing, missing, missing]) - @test all(lag(x, date, Day(1)) .=== [missing, missing, 5]) - date = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] - @test_throws ErrorException lag(x, date, Day(1)) + v = [4, 5, 6] + times = [1989, 1991, 1992] + @test all(lag(v, times) .=== [missing, missing, 5]) + @test all(lag(v, times; default = 0) .=== [0, 0, 5]) + @test all(lead(v, times) .=== [missing, 6, missing]) + @test all(lead(v, times; default = 0) .=== [0, 6, 0]) + times = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 4)] + @test all(lag(v, times, Day(1)) .=== [missing, missing, 5]) + @test all(lag(v, times, Day(2)) .=== [missing, 4, missing]) + @test all(lag(v, times, Day(5)) .=== [missing, missing, missing]) + @test all(lead(v, times, Day(1)) .=== [missing, 6, missing]) + @test all(lead(v, times, Day(1)) .=== lag(v, times, -Day(1))) + @test all(lead(v, times, Day(2)) .=== [5, missing, missing]) + @test all(lead(v, times, Day(5)) .=== [missing, missing, missing]) + times = [DateTime(1989, 1, 1), DateTime(1989, 1, 3), DateTime(1989, 1, 4)] + @test all(lag(v, times, Millisecond(1)) .=== [missing, missing, missing]) + @test all(lag(v, times, Day(1)) .=== [missing, missing, 5]) + times = [Date(1989, 1, 1), Date(1989, 1, 3), Date(1989, 1, 3)] + @test_throws ErrorException lag(v, times, Day(1)) end From 879ed0be9e3a47355e4df619754bd1c2fbe5911b Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Fri, 24 Apr 2020 12:36:08 -0400 Subject: [PATCH 13/15] add comments --- src/lag.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/lag.jl b/src/lag.jl index cf8eb0f..c8e8661 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -131,6 +131,8 @@ julia> lag(v, times, Day(1)) 3 """ function lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) + # Note that oneunit(zero(Date)) = Day(1) in Julia 1.5 + # Code follows the function indexin inds = keys(times) timesdict = Dict{eltype(times),eltype(inds)}() for (val, ind) in zip(times, inds) From 5d3ead950f08f0101f502805cc1b0246eddbc5e7 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Fri, 24 Apr 2020 14:31:46 -0400 Subject: [PATCH 14/15] multiple lines --- src/lag.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index c8e8661..788c677 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -138,8 +138,10 @@ function lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(elt for (val, ind) in zip(times, inds) out = get!(timesdict, val, ind) out != ind && error("Times must be distinct") - end - Union{eltype(v), typeof(default)}[(i = get(timesdict, x - period, nothing); i !== nothing ? v[i] : default) for x in times] + end + return Union{eltype(v), typeof(default)}[ + (i = get(timesdict, x - period, nothing); i !== nothing ? v[i] : default) for x in times + ] end """ From 6ecc3772301e0ddb9f46fa6c88c9a1d580043e4e Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Fri, 24 Apr 2020 16:46:40 -0400 Subject: [PATCH 15/15] Update lag.jl --- src/lag.jl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/lag.jl b/src/lag.jl index 788c677..b8ecfd0 100644 --- a/src/lag.jl +++ b/src/lag.jl @@ -106,7 +106,7 @@ lead(v::AbstractArray, n = 1; default = missing) = ShiftedArray(v, map(-, n); de """ - lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) -> Vector + lag(v::AbstractVector, times::AbstractVector, period = oneunit(eltype(times)); default = missing) -> Vector Shifts with respect to a times given in the vector `times`. The third variable `period` gives the period by which to shift. `default` specifies a default value when the shifted time is not in `times`. @@ -130,9 +130,8 @@ julia> lag(v, times, Day(1)) missing 3 """ -function lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) - # Note that oneunit(zero(Date)) = Day(1) in Julia 1.5 - # Code follows the function indexin +function lag(v::AbstractVector, times::AbstractVector, period = oneunit(eltype(times)); default = missing) + # Code follows the function indexin inds = keys(times) timesdict = Dict{eltype(times),eltype(inds)}() for (val, ind) in zip(times, inds) @@ -145,7 +144,7 @@ function lag(v::AbstractVector, times::AbstractVector, period = oneunit(zero(elt end """ - lead(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) -> Vector + lead(v::AbstractVector, times::AbstractVector, period = oneunit(eltype(times)); default = missing) -> Vector Shifts with respect to a vector of times `times`. The third variable `period` gives the period by which to shift. `default` specifies a default value when the shifted time is not in `times`. @@ -162,6 +161,6 @@ julia> lead(v, times, 1) 5 missing """ -function lead(v::AbstractVector, times::AbstractVector, period = oneunit(zero(eltype(times))); default = missing) +function lead(v::AbstractVector, times::AbstractVector, period = oneunit(eltype(times)); default = missing) lag(v, times, -period; default = default) end