From e65bcc641703cdea5f6e9aeb940d083cc2dfa653 Mon Sep 17 00:00:00 2001 From: Emmanuel-R8 Date: Wed, 18 May 2022 11:52:12 +0800 Subject: [PATCH 1/6] Add constructor. VSCode added to gitignore. --- .gitignore | 5 ++++- src/TS.jl | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b56ebaf..d5d9848 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ /Manifest.toml /docs/Manifest.toml /docs/build/ -/test/Manifest.toml \ No newline at end of file +/test/Manifest.toml + +# Editors +.vscode diff --git a/src/TS.jl b/src/TS.jl index 80090b7..a665188 100644 --- a/src/TS.jl +++ b/src/TS.jl @@ -278,3 +278,6 @@ function TS(coredata::AbstractArray{T,2}, index::AbstractVector{V}) where {T, V} df = DataFrame(coredata, :auto, copycols=true) TS(df, index) end + +# To avoid error on using a TS to create a TS +TS(ts::TS) = ts From 313d5ee54bdf4ead739d53ca6f493c89c20ced9b Mon Sep 17 00:00:00 2001 From: Emmanuel-R8 Date: Wed, 18 May 2022 12:33:23 +0800 Subject: [PATCH 2/6] Add Base.copy and Base.similar --- src/TS.jl | 10 +++--- src/TSx.jl | 6 +++- src/utils.jl | 95 +++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 97 insertions(+), 14 deletions(-) diff --git a/src/TS.jl b/src/TS.jl index a665188..223b60c 100644 --- a/src/TS.jl +++ b/src/TS.jl @@ -201,10 +201,10 @@ julia> ts = TS([random(10) random(10)], dates) # matrix object """ struct TS - coredata :: DataFrame + coredata::DataFrame # From DataFrame, index number/name/symbol - function TS(coredata::DataFrame, index::Union{String, Symbol, Int}) + function TS(coredata::DataFrame, index::Union{String,Symbol,Int}) if (DataFrames.ncol(coredata) == 1) TS(coredata, collect(Base.OneTo(DataFrames.nrow(coredata)))) end @@ -219,7 +219,7 @@ struct TS end # From DataFrame, external index - function TS(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int, TimeType}} + function TS(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int,TimeType}} sorted_index = sort(index) cd = copy(coredata) @@ -255,7 +255,7 @@ function TS(coredata::DataFrame, index::UnitRange{Int}) end # From AbstractVector -function TS(coredata::AbstractVector{T}, index::AbstractVector{V}) where {T, V} +function TS(coredata::AbstractVector{T}, index::AbstractVector{V}) where {T,V} df = DataFrame([coredata], :auto) TS(df, index) end @@ -274,7 +274,7 @@ function TS(coredata::AbstractArray{T,2}) where {T} TS(df, index_vals) end -function TS(coredata::AbstractArray{T,2}, index::AbstractVector{V}) where {T, V} +function TS(coredata::AbstractArray{T,2}, index::AbstractVector{V}) where {T,V} df = DataFrame(coredata, :auto, copycols=true) TS(df, index) end diff --git a/src/TSx.jl b/src/TSx.jl index 035ec91..3a89d96 100644 --- a/src/TSx.jl +++ b/src/TSx.jl @@ -2,6 +2,7 @@ module TSx using DataFrames, Dates, ShiftedArrays, RecipesBase, RollingFunctions +import Base.copy import Base.convert import Base.diff import Base.filter @@ -16,8 +17,9 @@ import Base.names import Base.print import Base.== import Base.show -import Base.summary +import Base.similar import Base.size +import Base.summary import Base.vcat import Dates.Period @@ -30,6 +32,7 @@ export TS, Matrix, apply, convert, + copy, cbind, describe, diff, @@ -55,6 +58,7 @@ export TS, log, rbind, show, + similar, size, subset, summary, diff --git a/src/utils.jl b/src/utils.jl index 7a951f2..d84eb24 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -199,7 +199,7 @@ julia> names(TS([1:10 11:20])) "x2" ``` """ - + function names(ts::TS) names(ts.coredata[!, Not(:Index)]) end @@ -226,7 +226,7 @@ julia> first(TS(1:10)) ``` """ function Base.first(ts::TS) - TS(Base.first(ts.coredata,1)) + TS(Base.first(ts.coredata, 1)) end @@ -242,8 +242,8 @@ Returns the first `n` rows of `ts`. julia> head(TS(1:100)) (10 x 1) TS with Int64 Index - Index x1 - Int64 Int64 + Index x1 + Int64 Int64 ────────────── 1 1 2 2 @@ -257,7 +257,7 @@ julia> head(TS(1:100)) 10 10 ``` """ -function head(ts::TS, n::Int = 10) +function head(ts::TS, n::Int=10) TS(Base.first(ts.coredata, n)) end @@ -274,8 +274,8 @@ Returns the last `n` rows of `ts`. julia> tail(TS(1:100)) (10 x 1) TS with Int64 Index - Index x1 - Int64 Int64 + Index x1 + Int64 Int64 ────────────── 91 91 92 92 @@ -289,6 +289,85 @@ julia> tail(TS(1:100)) 100 100 ``` """ -function tail(ts::TS, n::Int = 10) +function tail(ts::TS, n::Int=10) TS(DataFrames.last(ts.coredata, n)) end + + +# Base.copy +""" +# Base.copy +```julia +copy(ts::TS; copycols::Bool=true) +``` + +Copy the time series `ts` following the semantics of `copy(::DataFrame)`. If `copycols=true` (the default), +return a new `TS` holding copies of column vectors in `ts`. If `copycols=false`, return a new +`TS` sharing column vectors with `ts`. + +```jldoctest; setup = :(using TSx, DataFrames, Dates, Random) +julia> copy(TS(1:10)) +(10 x 1) TS with Int64 Index + + Index x1 + Int64 Int64 +────────────── + 1 1 + 2 2 + 3 3 + 4 4 + 5 5 + 6 6 + 7 7 + 8 8 + 9 9 + 10 10 +``` +""" +Base.copy(ts::TS; copycols::Bool=true) = TS(copy(ts.coredata; copycols)) + + +# Base.similar +""" +# Base.copy +```julia +similar(ts::TS, rows::Integer=nrow(ts)) +``` + +Create a new `TS` with the same column names and column element types as `ts`. An optional +second argument can be provided to request a number of rows that is different than the number + of rows present in `ts`. + +```jldoctest; setup = :(using TSx, DataFrames, Dates, Random) + +julia> similar(TS(1:10)) +(10 x 1) TS with Int64 Index + + Index x1 + Int64 Int64 +────────────────────────────────── + 3 3 + 3 3 + 20 20 + 32 32 + 32 32 + 100 100 + 250 250 + 1000 1000 + 3329 3329 + 140268692646400 140268692646400 + + julia> similar(TS(1:10), 4) +(4 x 1) TS with Int64 Index + + Index x1 + Int64 Int64 +────────────────────────────────── + 140268691629264 140267030569584 + 140268691701168 140267030569616 + 140268691701168 140267030569648 + 140268691731376 140268776059472 + +``` +""" +Base.similar(ts::TS, rows::Integer=nrow(ts)) = TS(similar(ts.coredata, rows)) From dc61e4b81e44b6667f872a0a7b63c87244db08c4 Mon Sep 17 00:00:00 2001 From: Emmanuel-R8 Date: Wed, 18 May 2022 12:38:58 +0800 Subject: [PATCH 3/6] Typos --- src/utils.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index d84eb24..0809907 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -296,7 +296,8 @@ end # Base.copy """ -# Base.copy +# Copy + ```julia copy(ts::TS; copycols::Bool=true) ``` @@ -329,7 +330,8 @@ Base.copy(ts::TS; copycols::Bool=true) = TS(copy(ts.coredata; copycols)) # Base.similar """ -# Base.copy +# Similar + ```julia similar(ts::TS, rows::Integer=nrow(ts)) ``` From dcda7c07cba5718279569a871b04ed5d06c98f4f Mon Sep 17 00:00:00 2001 From: Emmanuel_R8 <54389619+Emmanuel-R8@users.noreply.github.com> Date: Thu, 19 May 2022 08:27:34 +0800 Subject: [PATCH 4/6] Update TS.jl --- src/TS.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TS.jl b/src/TS.jl index 223b60c..d6455d2 100644 --- a/src/TS.jl +++ b/src/TS.jl @@ -201,7 +201,7 @@ julia> ts = TS([random(10) random(10)], dates) # matrix object """ struct TS - coredata::DataFrame + coredata :: DataFrame # From DataFrame, index number/name/symbol function TS(coredata::DataFrame, index::Union{String,Symbol,Int}) From a2645f5e0b1f0d152e1e08388b6b867f89e95d4e Mon Sep 17 00:00:00 2001 From: Emmanuel_R8 <54389619+Emmanuel-R8@users.noreply.github.com> Date: Thu, 19 May 2022 08:29:43 +0800 Subject: [PATCH 5/6] Update TS.jl --- src/TS.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/TS.jl b/src/TS.jl index d6455d2..a665188 100644 --- a/src/TS.jl +++ b/src/TS.jl @@ -204,7 +204,7 @@ struct TS coredata :: DataFrame # From DataFrame, index number/name/symbol - function TS(coredata::DataFrame, index::Union{String,Symbol,Int}) + function TS(coredata::DataFrame, index::Union{String, Symbol, Int}) if (DataFrames.ncol(coredata) == 1) TS(coredata, collect(Base.OneTo(DataFrames.nrow(coredata)))) end @@ -219,7 +219,7 @@ struct TS end # From DataFrame, external index - function TS(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int,TimeType}} + function TS(coredata::DataFrame, index::AbstractVector{T}) where {T<:Union{Int, TimeType}} sorted_index = sort(index) cd = copy(coredata) @@ -255,7 +255,7 @@ function TS(coredata::DataFrame, index::UnitRange{Int}) end # From AbstractVector -function TS(coredata::AbstractVector{T}, index::AbstractVector{V}) where {T,V} +function TS(coredata::AbstractVector{T}, index::AbstractVector{V}) where {T, V} df = DataFrame([coredata], :auto) TS(df, index) end @@ -274,7 +274,7 @@ function TS(coredata::AbstractArray{T,2}) where {T} TS(df, index_vals) end -function TS(coredata::AbstractArray{T,2}, index::AbstractVector{V}) where {T,V} +function TS(coredata::AbstractArray{T,2}, index::AbstractVector{V}) where {T, V} df = DataFrame(coredata, :auto, copycols=true) TS(df, index) end From 55f4821e6d301a8fe9a8fc7cc30f7b649a9d8480 Mon Sep 17 00:00:00 2001 From: Emmanuel_R8 <54389619+Emmanuel-R8@users.noreply.github.com> Date: Thu, 19 May 2022 08:31:02 +0800 Subject: [PATCH 6/6] Update utils.jl --- src/utils.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index 0809907..00f5f21 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -226,7 +226,7 @@ julia> first(TS(1:10)) ``` """ function Base.first(ts::TS) - TS(Base.first(ts.coredata, 1)) + TS(Base.first(ts.coredata,1)) end @@ -257,7 +257,7 @@ julia> head(TS(1:100)) 10 10 ``` """ -function head(ts::TS, n::Int=10) +function head(ts::TS, n::Int = 10) TS(Base.first(ts.coredata, n)) end @@ -289,7 +289,7 @@ julia> tail(TS(1:100)) 100 100 ``` """ -function tail(ts::TS, n::Int=10) +function tail(ts::TS, n::Int = 10) TS(DataFrames.last(ts.coredata, n)) end @@ -372,4 +372,4 @@ julia> similar(TS(1:10)) ``` """ -Base.similar(ts::TS, rows::Integer=nrow(ts)) = TS(similar(ts.coredata, rows)) +Base.similar(ts::TS, rows::Integer=nrow(ts)) = TS(DataFrames::similar(ts.coredata, rows))