diff --git a/dev/api/index.html b/dev/api/index.html index e8dc8b1..74d5f30 100644 --- a/dev/api/index.html +++ b/dev/api/index.html @@ -30,7 +30,7 @@ 0.586022 0.586022 0.0521332 0.0521332 0.26864 0.26864 - 0.108871 0.108871source
TSFrames.TSFrameType
struct TSFrame
+ 0.108871   0.108871
source
TSFrames.TSFrameType
struct TSFrame
   coredata :: DataFrame
 end

::TSFrame - A type to hold ordered data with an index.

A TSFrame object is essentially a DataFrame with a specific column marked as an index. The input DataFrame is sorted during construction and is stored under the property coredata. The index is stored in the Index column of coredata.

Permitted data inputs to the constructors are DataFrame, Vector, and 2-dimensional Array. If an index is already not present in the constructor then a sequential integer index is created automatically.

TSFrame(coredata::DataFrame): Here, the constructor looks for a column named Index in coredata as the index column, if this is not found then the first column of coredata is made the index by default. If coredata only has a single column then a new sequential index is generated.

Since TSFrame.coredata is a DataFrame it can be operated upon independently using methods provided by the DataFrames package (ex. transform, combine, etc.).

Constructors

TSFrame(coredata::DataFrame, index::Union{String, Symbol, Int}; issorted = false, copycols = true)
 TSFrame(coredata::DataFrame, index::AbstractVector{T}; issorted = false, copycols = true) where {T<:Union{Int, TimeType}}
@@ -304,18 +304,18 @@
 
 julia> ts = TSFrame(Date, [(Int64, "col1"), (String, "col2"), (Float64, "col3")]) # using strings instead of symbols
 0×3 TSFrame with Date Index
-
source
Base.:==Method

Equality

Two TSFrame are considered equal if their `coredata` property is equal.
+
source
Base.:==Method

Equality

Two TSFrame are considered equal if their `coredata` property is equal.
 Base.:(==)(tsf1::TSFrame, tsf2::TSFrame)::Bool
-Base.isequal(tsf1::TSFrame, tsf2::TSFrame)::Bool
source
Base.firstMethod

First Row

first(ts::TSFrame)

Returns the first row of ts as a TSFrame object.

Examples

julia> first(TSFrame(1:10))
+Base.isequal(tsf1::TSFrame, tsf2::TSFrame)::Bool
source
Base.firstMethod

First Row

first(ts::TSFrame)

Returns the first row of ts as a TSFrame object.

Examples

julia> first(TSFrame(1:10))
 (10 x 1) TSFrame with Dates.Date Index
 
  Index       x1
  Date        Float64
 ───────────────────────
  2022-02-01  0.768448
-
source
Base.iterateFunction

Iterators

Base.iterate(tsf::TSFrame)
+
source
Base.iterateFunction

Iterators

Base.iterate(tsf::TSFrame)
 
-Returns a row-based iterator for `tsf`.
source
Base.joinMethod

Joins/Column-binding

join(ts1::TSFrame, ts2::TSFrame, ts...; jointype::Symbol=:JoinAll)

TSFrame objects can be combined together column-wise using Index as the column key. There are four kinds of column-binding operations possible as of now. Each join operation works by performing a Set operation on the Index column and then merging the datasets based on the output from the Set operation. Each operation changes column names in the final object automatically if the operation encounters duplicate column names amongst the TSFrame objects.

The following join types are supported:

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinInner) and join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinBoth)

a.k.a. inner join, takes the intersection of the indexes of ts1 and ts2, and then merges the columns of both the objects. The resulting object will only contain rows which are present in both the objects' indexes. The function will rename columns in the final object if they had same names in the TSFrame objects.

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinOuter) and join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinAll):

a.k.a. outer join, takes the union of the indexes of ts1 and ts2 before merging the other columns of input objects. The output will contain rows which are present in all the input objects while inserting missing values where a row was not present in any of the objects. This is the default behaviour if no jointype is provided.

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinLeft):

Left join takes the index values which are present in the left object ts1 and finds matching index values in the right object ts2. The resulting object includes all the rows from the left object, the column values from the left object, and the values associated with matching index rows on the right. The operation inserts missing values where in the unmatched rows of the right object.

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinRight)

Right join, similar to left join but works in the opposite direction. The final object contains all the rows from the right object while inserting missing values in rows missing from the left object.

The default behaviour is to assume jointype=:JoinAll if no jointype is provided to the join method.

Joining multiple TSFrames is also supported. The syntax is

join(ts1::TSFrame, ts2::TSFrame, ts...; jointype::Symbol)

where jointype must be one of :JoinInner, :JoinBoth, :JoinOuter, :JoinAll, :JoinLeft or :JoinRight. Note that join on multiple TSFrames is left associative.

cbind is an alias for join method.

Examples

julia> using Random;
+Returns a row-based iterator for `tsf`.
source
Base.joinMethod

Joins/Column-binding

join(ts1::TSFrame, ts2::TSFrame, ts...; jointype::Symbol=:JoinAll)

TSFrame objects can be combined together column-wise using Index as the column key. There are four kinds of column-binding operations possible as of now. Each join operation works by performing a Set operation on the Index column and then merging the datasets based on the output from the Set operation. Each operation changes column names in the final object automatically if the operation encounters duplicate column names amongst the TSFrame objects.

The following join types are supported:

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinInner) and join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinBoth)

a.k.a. inner join, takes the intersection of the indexes of ts1 and ts2, and then merges the columns of both the objects. The resulting object will only contain rows which are present in both the objects' indexes. The function will rename columns in the final object if they had same names in the TSFrame objects.

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinOuter) and join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinAll):

a.k.a. outer join, takes the union of the indexes of ts1 and ts2 before merging the other columns of input objects. The output will contain rows which are present in all the input objects while inserting missing values where a row was not present in any of the objects. This is the default behaviour if no jointype is provided.

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinLeft):

Left join takes the index values which are present in the left object ts1 and finds matching index values in the right object ts2. The resulting object includes all the rows from the left object, the column values from the left object, and the values associated with matching index rows on the right. The operation inserts missing values where in the unmatched rows of the right object.

join(ts1::TSFrame, ts2::TSFrame; jointype=:JoinRight)

Right join, similar to left join but works in the opposite direction. The final object contains all the rows from the right object while inserting missing values in rows missing from the left object.

The default behaviour is to assume jointype=:JoinAll if no jointype is provided to the join method.

Joining multiple TSFrames is also supported. The syntax is

join(ts1::TSFrame, ts2::TSFrame, ts...; jointype::Symbol)

where jointype must be one of :JoinInner, :JoinBoth, :JoinOuter, :JoinAll, :JoinLeft or :JoinRight. Note that join on multiple TSFrames is left associative.

cbind is an alias for join method.

Examples

julia> using Random;
 
 julia> random(x) = rand(MersenneTwister(123), x);
 
@@ -527,8 +527,8 @@
  2017-01-08  0.0521332  0.0521332  0.0521332
  2017-01-09  0.26864    0.26864    0.26864
  2017-01-10  0.108871   0.108871   0.108871
-
source
Base.sizeMethod

Size methods

size(ts::TSFrame)

Return the number of rows and columns of ts as a tuple.

Examples

julia> TSFrames.size(TSFrame([collect(1:100) collect(1:100) collect(1:100)]))
-(100, 3)
source
Base.vcatMethod

Row-merging (vcat/rbind)

vcat(ts1::TSFrame, ts2::TSFrame; colmerge::Symbol=:union)

Concatenate rows of two TSFrame objects, append ts2 to ts1.

The colmerge keyword argument specifies the column merge strategy. The value of colmerge is directly passed to cols argument of DataFrames.vcat.

Currently, DataFrames.vcat supports four types of column-merge strategies:

  1. :setequal: only merge if both objects have same column names, use the order of columns in ts1.
  2. :orderequal: only merge if both objects have same column names and columns are in the same order.
  3. :intersect: only merge the columns which are common to both objects, ignore the rest.
  4. :union: merge even if columns differ, the resulting object has all the columns filled with missing, if necessary.

Examples

julia> using Random;
+
source
Base.sizeMethod

Size methods

size(ts::TSFrame)

Return the number of rows and columns of ts as a tuple.

Examples

julia> TSFrames.size(TSFrame([collect(1:100) collect(1:100) collect(1:100)]))
+(100, 3)
source
Base.vcatMethod

Row-merging (vcat/rbind)

vcat(ts1::TSFrame, ts2::TSFrame; colmerge::Symbol=:union)

Concatenate rows of two TSFrame objects, append ts2 to ts1.

The colmerge keyword argument specifies the column merge strategy. The value of colmerge is directly passed to cols argument of DataFrames.vcat.

Currently, DataFrames.vcat supports four types of column-merge strategies:

  1. :setequal: only merge if both objects have same column names, use the order of columns in ts1.
  2. :orderequal: only merge if both objects have same column names and columns are in the same order.
  3. :intersect: only merge the columns which are common to both objects, ignore the rest.
  4. :union: merge even if columns differ, the resulting object has all the columns filled with missing, if necessary.

Examples

julia> using Random;
 
 julia> random(x) = rand(MersenneTwister(123), x);
 
@@ -655,7 +655,7 @@
  2017-01-28   0.278259
  2017-01-29   0.0231765
  2017-01-30   0.516704
-
source
RecipesBase.apply_recipeFunction

Plotting

plot(ts::TSFrame, cols::Vector{Int} = collect(1:TSFrames.ncol(ts)))
+
source
RecipesBase.apply_recipeFunction

Plotting

plot(ts::TSFrame, cols::Vector{Int} = collect(1:TSFrames.ncol(ts)))
 plot(ts::TSFrame, cols::Vector{T}) where {T<:Union{String, Symbol}}
 plot(ts::TSFrame, cols::T) where {T<:Union{Int, String, Symbol}}

Plots a TSFrame object with the index on the x-axis and selected cols on the y-axis. By default, plot all the columns. Columns can be selected using Int indexes, String(s), or Symbol(s).

Example

julia> using Random;
 julia> random(x) = rand(MersenneTwister(123), x);
@@ -695,7 +695,7 @@
 julia> # plot(ts[1:6], [:val1, :val3]);
 
 # plot columns 1 and 2 on a specified window size
-julia> # plot(ts, [1, 2], size=(600, 400));
source
TSFrames._check_consistencyMethod

Internal function to check consistency of the Index of a TSFrame object.

source
TSFrames.applyMethod

Apply/Period conversion

apply(ts::TSFrame,
+julia> # plot(ts, [1, 2], size=(600, 400));
source
TSFrames._check_consistencyMethod

Internal function to check consistency of the Index of a TSFrame object.

source
TSFrames.applyMethod

Apply/Period conversion

apply(ts::TSFrame,
       period::T,
       fun::V,
       index_at::Function=first;
@@ -797,7 +797,7 @@
  2017-02-19    0.319216
  2017-02-26    0.272058
  2017-03-05    0.23651
-
source
TSFrames.describeMethod

Summary statistics

describe(ts::TSFrame; cols=:)
+
source
TSFrames.describeMethod

Summary statistics

describe(ts::TSFrame; cols=:)
 describe(ts::TSFrame, stats::Union{Symbol, Pair}...; cols=:)

Compute summary statistics of ts. The output is a DataFrame containing standard statistics along with number of missing values and data types of columns. The cols keyword controls which subset of columns from ts to be selected. The stats keyword is used to control which summary statistics are to be printed. For more information about these keywords, check out the corresponding documentation from DataFrames.jl.

Examples

julia> using Random;
 julia> random(x) = rand(MersenneTwister(123), x...);
 julia> ts = TSFrame(random(([1, 2, 3, 4, missing], 10)))
@@ -833,7 +833,7 @@
      │ Symbol    Int64  Int64
 ─────┼────────────────────────
    1 │ x1            2     22
-
source
TSFrames.endpointsFunction

Computing end points

endpoints(timestamps::AbstractVector{T}, on::V) where {T<:Union{Date, DateTime, Time},
+
source
TSFrames.endpointsFunction

Computing end points

endpoints(timestamps::AbstractVector{T}, on::V) where {T<:Union{Date, DateTime, Time},
                                                        V<:Union{
                                                            Year,
                                                            Quarter,
@@ -1079,7 +1079,7 @@
  2022-10-08T11:56:46
  2022-10-08T12:32:08
  2022-10-08T13:59:08
- 2022-10-08T14:37:17
source
TSFrames.headFunction

Head

head(ts::TSFrame, n::Int = 10)

Returns the first n rows of ts.

Examples

julia> head(TSFrame(1:100))
+ 2022-10-08T14:37:17
source
TSFrames.headFunction

Head

head(ts::TSFrame, n::Int = 10)

Returns the first n rows of ts.

Examples

julia> head(TSFrame(1:100))
 (10 x 1) TSFrame with Int64 Index
 
  Index  x1
@@ -1094,7 +1094,7 @@
      7      7
      8      8
      9      9
-    10     10
source
TSFrames.indexMethod

Index column

index(ts::TSFrame)

Return the index vector from the coredata DataFrame.

Examples

julia> using Random;
+    10     10
source
TSFrames.indexMethod

Index column

index(ts::TSFrame)

Return the index vector from the coredata DataFrame.

Examples

julia> using Random;
 
 julia> random(x) = rand(MersenneTwister(123), x);
 
@@ -1132,7 +1132,7 @@
  2022-11-01
 
 julia>  eltype(index(ts))
-Date
source
TSFrames.isregularMethod
isregular(timestamps::V, unit::T) where {V<:AbstractVector{TimeType}, T<:Dates.Period}
+Date
source
TSFrames.isregularMethod
isregular(timestamps::V, unit::T) where {V<:AbstractVector{TimeType}, T<:Dates.Period}
 isregular(timestamps::T) where {T<:AbstractVector{TimeType}}
 isregular(timestamps::AbstractVector{V}, unit::Symbol = :firstdiff) where {V<:TimeType}
 isregular(ts::TSFrame, unit::Symbol = :firstdiff)
@@ -1190,7 +1190,7 @@
 false
 
 julia> isregular(ts, :month)
-false
source
TSFrames.lagFunction

Lagging

lag(ts::TSFrame, lag_value::Int = 1)

Lag the ts object by the specified lag_value. The rows corresponding to lagged values will be rendered as missing. Negative values of lag are also accepted (see TSFrames.lead).

Examples

julia> using Random, Statistics;
+false
source
TSFrames.lagFunction

Lagging

lag(ts::TSFrame, lag_value::Int = 1)

Lag the ts object by the specified lag_value. The rows corresponding to lagged values will be rendered as missing. Negative values of lag are also accepted (see TSFrames.lead).

Examples

julia> using Random, Statistics;
 
 julia> random(x) = rand(MersenneTwister(123), x);
 
@@ -1248,7 +1248,7 @@
  2017-01-08        0.662555
  2017-01-09        0.586022
  2017-01-10        0.0521332
-
source
TSFrames.leadFunction

Leading

lead(ts::TSFrame, lead_value::Int = 1)

Similar to lag, this method leads the ts object by lead_value. The lead rows are inserted with missing. Negative values of lead are also accepted (see TSFrames.lag).

Examples

julia> using Random, Statistics;
+
source
TSFrames.leadFunction

Leading

lead(ts::TSFrame, lead_value::Int = 1)

Similar to lag, this method leads the ts object by lead_value. The lead rows are inserted with missing. Negative values of lead are also accepted (see TSFrames.lag).

Examples

julia> using Random, Statistics;
 
 julia> random(x) = rand(MersenneTwister(123), x);
 
@@ -1314,7 +1314,7 @@
  2017-01-08  0.108871
  2017-01-09  0.163666
  2017-01-10  0.473017
-
source
TSFrames.ncolMethod

Size methods

ncol(ts::TSFrame)

Return the number of columns of ts. nc is an alias for ncol.

Examples

julia> using Random;
+
source
TSFrames.ncolMethod

Size methods

ncol(ts::TSFrame)

Return the number of columns of ts. nc is an alias for ncol.

Examples

julia> using Random;
 
 julia> random(x) = rand(MersenneTwister(123), x);
 
@@ -1322,10 +1322,10 @@
 3
 
 julia> nc(TSFrame([random(100) random(100) random(100)]))
-3
source
TSFrames.nrowMethod

Size methods

nrow(ts::TSFrame)
+3
source
TSFrames.nrowMethod

Size methods

nrow(ts::TSFrame)
 nr(ts::TSFrame)

Return the number of rows of ts. nr is an alias for nrow.

Examples

julia> ts = TSFrame(collect(1:10))
 julia> TSFrames.nrow(ts)
-10
source
TSFrames.rename!Method

Column Rename

rename!(ts::TSFrame, colnames::AbstractVector{String}; makeunique::Bool=false)
+10
source
TSFrames.rename!Method

Column Rename

rename!(ts::TSFrame, colnames::AbstractVector{String}; makeunique::Bool=false)
 rename!(ts::TSFrame, colnames::AbstractVector{Symbol}; makeunique::Bool=false)
 rename!(ts::TSFrame, (from => to)::Pair...)
 rename!(ts::TSFrame, d::AbstractDict)
@@ -1441,7 +1441,7 @@
  2012-01-08      8     18
  2012-01-09      9     19
  2012-01-10     10     20
-
source
TSFrames.rollapplyMethod

Rolling Functions

rollapply(ts::TSFrame, fun::Function, windowsize::Int; bycolumn=true)

Apply function fun to rolling windows of ts. The output is a TSFrame object with (nrow(ts) - windowsize + 1) rows indexed with the last index value of each window.

The bycolumn argument should be set to true (default) if fun is to be applied to each column separately, and to false if fun takes a whole TSFrame as an input.

Examples

julia> rollapply(TSFrame([1:10 11:20]), mean, 5)
+
source
TSFrames.rollapplyMethod

Rolling Functions

rollapply(ts::TSFrame, fun::Function, windowsize::Int; bycolumn=true)

Apply function fun to rolling windows of ts. The output is a TSFrame object with (nrow(ts) - windowsize + 1) rows indexed with the last index value of each window.

The bycolumn argument should be set to true (default) if fun is to be applied to each column separately, and to false if fun takes a whole TSFrame as an input.

Examples

julia> rollapply(TSFrame([1:10 11:20]), mean, 5)
 6×2 TSFrame with Int64 Index
  Index  rolling_x1_mean  rolling_x2_mean 
  Int64  Float64          Float64         
@@ -1488,7 +1488,7 @@
  2001-01-07  ([1.0], 4.86475e-16)
  2001-01-08  ([1.0], 7.43103e-16)
  2001-01-09  ([1.0], 7.45753e-15)
- 2001-01-10  ([1.0], 9.28561e-15)
source
TSFrames.subsetMethod

Subsetting based on Index

subset(ts::TSFrame, from::T, to::T) where {T<:Union{Int, TimeType}}

Create a subset of ts based on the Index starting from (inclusive) till to (inclusive).

Examples

julia> using Random;
+ 2001-01-10  ([1.0], 9.28561e-15)
source
TSFrames.subsetMethod

Subsetting based on Index

subset(ts::TSFrame, from::T, to::T) where {T<:Union{Int, TimeType}}

Create a subset of ts based on the Index starting from (inclusive) till to (inclusive).

Examples

julia> using Random;
 julia> random(x) = rand(MersenneTwister(123), x);
 julia> dates = Date("2022-02-01"):Week(1):Date("2022-02-01")+Month(9);
 julia> ts = TSFrame(random(length(dates)), dates)
@@ -1612,7 +1612,7 @@
  2022-10-25  0.621379
  2022-11-01  0.348173
 
-
source
TSFrames.tailFunction

Tail

tail(ts::TSFrame, n::Int = 10)

Returns the last n rows of ts.

julia> tail(TSFrame(1:100))
+
source
TSFrames.tailFunction

Tail

tail(ts::TSFrame, n::Int = 10)

Returns the last n rows of ts.

julia> tail(TSFrame(1:100))
 (10 x 1) TSFrame with Int64 Index
 
  Index  x1
@@ -1627,7 +1627,7 @@
     97     97
     98     98
     99     99
-   100    100
source
TSFrames.to_periodMethod

Frequency conversion

Set of convenience methods for frequency conversion of TimeType index types. Internally, they call endpoints() to do the actual conversion. n is the number of periods of the period type. For example, to_monthly(tsf, 2) will resample the time series to "every 2 months".

to_period(tsf::TSFrame, period::T)::TSFrame where {T<:Period}
+   100    100
source
TSFrames.to_periodMethod

Frequency conversion

Set of convenience methods for frequency conversion of TimeType index types. Internally, they call endpoints() to do the actual conversion. n is the number of periods of the period type. For example, to_monthly(tsf, 2) will resample the time series to "every 2 months".

to_period(tsf::TSFrame, period::T)::TSFrame where {T<:Period}
 to_yearly(tsf::TSFrame, n=1)::TSFrame
 to_quarterly(tsf::TSFrame, n=1)::TSFrame
 to_monthly(tsf::TSFrame, n=1)::TSFrame
@@ -1638,4 +1638,4 @@
 to_seconds(tsf::TSFrame, n=1)::TSFrame
 to_milliseconds(tsf::TSFrame, n=1)::TSFrame
 to_microseconds(tsf::TSFrame, n=1)::TSFrame
-to_nanoseconds(tsf::TSFrame, n=1)::TSFrame
source
+to_nanoseconds(tsf::TSFrame, n=1)::TSFramesource diff --git a/dev/demo_finance/index.html b/dev/demo_finance/index.html index 0d099b6..df3605d 100644 --- a/dev/demo_finance/index.html +++ b/dev/demo_finance/index.html @@ -26,7 +26,7 @@ ──────────────────────────────────────────────────────────────────────────────── 1980-12-12 0.128348 0.128906 0.128348 0.128348 0.099192 4.690 ⋯ 1980-12-15 0.12221 0.12221 0.121652 0.121652 0.094017 1.758 - 1980-12-16 0.113281 0.113281 0.112723 0.112723 0.087117 1.057 + 1980-12-16 0.113281 0.113281 0.112723 0.112723 0.087116 1.057 1980-12-17 0.115513 0.116071 0.115513 0.115513 0.089273 8.644 1980-12-18 0.118862 0.11942 0.118862 0.118862 0.091861 7.344 ⋯ 1980-12-19 0.126116 0.126674 0.126116 0.126116 0.097467 4.863 @@ -145,86 +145,86 @@ Date Float64? Float64? ──────────────────────────────────────────────── 2021-06-04 missing missing - 2021-06-11 0.0258465 0.0115306 - 2021-06-18 -0.0554489 0.0241276 - 2021-06-25 0.0256601 0.0201089 - 2021-07-02 -0.0475582 0.0501811 - 2021-07-09 0.0106559 0.0361353 - 2021-07-16 -0.018687 0.00878197 - 2021-07-23 0.017414 0.0147148 + 2021-06-11 0.0258467 0.0115308 + 2021-06-18 -0.0554491 0.0241274 + 2021-06-25 0.0256603 0.020109 + 2021-07-02 -0.0475582 0.0501812 + 2021-07-09 0.0106556 0.0361352 + 2021-07-16 -0.0186869 0.00878229 + 2021-07-23 0.0174141 0.0147144 ⋮ ⋮ ⋮ - 2021-11-19 -0.0247662 0.0680369 - 2021-11-26 -0.00207031 -0.0235706 - 2021-12-03 0.0258272 0.0315732 + 2021-11-19 -0.0247661 0.0680367 + 2021-11-26 -0.0020703 -0.0235705 + 2021-12-03 0.0258271 0.0315733 2021-12-10 0.0432291 0.103288 2021-12-17 0.0263246 -0.0474146 - 2021-12-23 0.0250371 0.0295919 - 2021-12-31 0.0229303 0.00729127 + 2021-12-23 0.0250371 0.0295917 + 2021-12-31 0.0229303 0.00729144 16 rows omitted
julia> TSFrames.rename!(ibm_aapl_weekly_returns, [:IBM, :AAPL])31×2 TSFrame with Date Index Index IBM AAPL Date Float64? Float64? ──────────────────────────────────────────────── 2021-06-04 missing missing - 2021-06-11 0.0258465 0.0115306 - 2021-06-18 -0.0554489 0.0241276 - 2021-06-25 0.0256601 0.0201089 - 2021-07-02 -0.0475582 0.0501811 - 2021-07-09 0.0106559 0.0361353 - 2021-07-16 -0.018687 0.00878197 - 2021-07-23 0.017414 0.0147148 + 2021-06-11 0.0258467 0.0115308 + 2021-06-18 -0.0554491 0.0241274 + 2021-06-25 0.0256603 0.020109 + 2021-07-02 -0.0475582 0.0501812 + 2021-07-09 0.0106556 0.0361352 + 2021-07-16 -0.0186869 0.00878229 + 2021-07-23 0.0174141 0.0147144 ⋮ ⋮ ⋮ - 2021-11-19 -0.0247662 0.0680369 - 2021-11-26 -0.00207031 -0.0235706 - 2021-12-03 0.0258272 0.0315732 + 2021-11-19 -0.0247661 0.0680367 + 2021-11-26 -0.0020703 -0.0235705 + 2021-12-03 0.0258271 0.0315733 2021-12-10 0.0432291 0.103288 2021-12-17 0.0263246 -0.0474146 - 2021-12-23 0.0250371 0.0295919 - 2021-12-31 0.0229303 0.00729127 - 16 rows omitted

Compute standard deviation of weekly returns

Computing standard deviation is done using the std function from Statistics package. The skipmissing is used to skip missing values which may have been generated while computing log returns or were already present in the data.

julia> ibm_std = std(skipmissing(ibm_aapl_weekly_returns[:, :IBM]))0.03407927410417811
julia> aapl_std = std(skipmissing(ibm_aapl_weekly_returns[:, :AAPL]))0.030582296043825258

Scatter plot of AAPL and IBM

Here, we use the Plots package to create a scatter plot with IBM weekly returns on the x-axis and Apple weekly returns on the y-axis.

plot(ibm_aapl_weekly_returns[:, :AAPL],
+ 2021-12-23        0.0250371         0.0295917
+ 2021-12-31        0.0229303         0.00729144
+                                 16 rows omitted

Compute standard deviation of weekly returns

Computing standard deviation is done using the std function from Statistics package. The skipmissing is used to skip missing values which may have been generated while computing log returns or were already present in the data.

julia> ibm_std = std(skipmissing(ibm_aapl_weekly_returns[:, :IBM]))0.034079299383423495
julia> aapl_std = std(skipmissing(ibm_aapl_weekly_returns[:, :AAPL]))0.030582290427209696

Scatter plot of AAPL and IBM

Here, we use the Plots package to create a scatter plot with IBM weekly returns on the x-axis and Apple weekly returns on the y-axis.

plot(ibm_aapl_weekly_returns[:, :AAPL],
     ibm_aapl_weekly_returns[:, :IBM],
     seriestype = :scatter;
     xlabel = "AAPL",
     ylabel = "IBM",
     legend = false)

Aggregation and rolling window operations

Here, we compute realised volatility of returns of both IBM and Apple stock weekly and bi-monthly. Then, we compute daily returns volatility on a rolling basis with a window size of 10.

julia> daily_returns = diff(log.(ibm_aapl))150×2 TSFrame with Date Index
- Index       IBM_log            AAPL_log
- Date        Float64?           Float64?
-──────────────────────────────────────────────────
- 2021-06-01  missing            missing
- 2021-06-02        0.0105552          0.00625669
- 2021-06-03       -0.00116741        -0.0122288
- 2021-06-04        0.0127662          0.0188436
- 2021-06-07        0.00406161         7.94047e-5
- 2021-06-08        0.00706863         0.0066498
- 2021-06-09        0.0106759          0.0030723
- 2021-06-10       -0.000863257       -0.00805557
-     ⋮               ⋮                  ⋮
- 2021-12-22        0.00602969         0.0152026
- 2021-12-23        0.00675933         0.00363733
- 2021-12-27        0.00755007         0.0227149
- 2021-12-28        0.00764424        -0.00578402
- 2021-12-29        0.00541405         0.000502007
- 2021-12-30        0.00419066        -0.00660008
- 2021-12-31       -0.00186874        -0.00354151
-                                  135 rows omitted
julia> rvol = apply(daily_returns, Week(1), std) # Compute the realised volatility31×2 TSFrame with Date Index + Index IBM_log AAPL_log + Date Float64? Float64? +───────────────────────────────────────────────── + 2021-06-01 missing missing + 2021-06-02 0.0105551 0.00625657 + 2021-06-03 -0.00116735 -0.0122287 + 2021-06-04 0.0127661 0.0188436 + 2021-06-07 0.00406161 7.94693e-5 + 2021-06-08 0.00706863 0.00664985 + 2021-06-09 0.0106761 0.00307242 + 2021-06-10 -0.00086313 -0.00805569 + ⋮ ⋮ ⋮ + 2021-12-22 0.00602969 0.0152027 + 2021-12-23 0.0067594 0.00363725 + 2021-12-27 0.00755 0.0227149 + 2021-12-28 0.00764438 -0.00578402 + 2021-12-29 0.00541392 0.000502097 + 2021-12-30 0.00419079 -0.00660025 + 2021-12-31 -0.00186875 -0.00354125 + 135 rows omitted
julia> rvol = apply(daily_returns, Week(1), std) # Compute the realised volatility31×2 TSFrame with Date Index Index IBM_log_std AAPL_log_std Date Float64? Float64? ──────────────────────────────────────────────── 2021-06-01 missing missing - 2021-06-07 0.0042314 0.0068503 - 2021-06-14 0.00515149 0.0140528 - 2021-06-21 0.0135566 0.00849497 - 2021-06-28 0.022242 0.00681127 - 2021-07-06 0.0077468 0.0123477 - 2021-07-12 0.00554703 0.0145984 - 2021-07-19 0.00913885 0.0201179 + 2021-06-07 0.00423144 0.00685038 + 2021-06-14 0.00515155 0.0140528 + 2021-06-21 0.0135566 0.00849511 + 2021-06-28 0.022242 0.00681128 + 2021-07-06 0.0077469 0.0123476 + 2021-07-12 0.00554725 0.0145982 + 2021-07-19 0.00913881 0.0201179 ⋮ ⋮ ⋮ - 2021-11-15 0.00421519 0.0107342 - 2021-11-22 0.00524038 0.0175359 - 2021-11-29 0.0142162 0.0188585 - 2021-12-06 0.00430293 0.0142467 - 2021-12-13 0.0139501 0.0249442 - 2021-12-20 0.00719018 0.0122394 - 2021-12-27 0.00389372 0.0121978 + 2021-11-15 0.00421507 0.0107341 + 2021-11-22 0.00524026 0.017536 + 2021-11-29 0.014216 0.0188585 + 2021-12-06 0.00430291 0.0142466 + 2021-12-13 0.0139502 0.0249443 + 2021-12-20 0.0071901 0.0122394 + 2021-12-27 0.00389373 0.0121978 16 rows omitted
julia> rvol = apply(daily_returns, Month(2), std) # Every two months4×2 TSFrame with Date Index Index IBM_log_std AAPL_log_std Date Float64? Float64? @@ -232,26 +232,26 @@ 2021-06-01 missing missing 2021-08-02 0.00863796 0.0124996 2021-10-01 0.0187055 0.0131257 - 2021-12-01 0.00792972 0.0184033
julia> rollapply(daily_returns, std, 10) # Compute rolling vols141×2 TSFrame with Date Index + 2021-12-01 0.00792973 0.0184033
julia> rollapply(daily_returns, std, 10) # Compute rolling vols141×2 TSFrame with Date Index Index rolling_IBM_log_std rolling_AAPL_log_std Date Float64? Float64? ─────────────────────────────────────────────────────── 2021-06-14 missing missing - 2021-06-15 0.00703762 0.0116 - 2021-06-16 0.00777312 0.011578 - 2021-06-17 0.00939338 0.0103008 - 2021-06-18 0.00965323 0.0105066 - 2021-06-21 0.0128876 0.0109084 - 2021-06-22 0.0125776 0.0111713 - 2021-06-23 0.0121088 0.0114156 + 2021-06-15 0.00703763 0.0115999 + 2021-06-16 0.00777313 0.0115779 + 2021-06-17 0.00939336 0.0103008 + 2021-06-18 0.0096533 0.0105066 + 2021-06-21 0.0128877 0.0109084 + 2021-06-22 0.0125777 0.0111714 + 2021-06-23 0.0121089 0.0114156 ⋮ ⋮ ⋮ - 2021-12-22 0.0102034 0.022022 - 2021-12-23 0.0102076 0.0220114 - 2021-12-27 0.0102131 0.0213968 - 2021-12-28 0.0079822 0.0202205 - 2021-12-29 0.00799231 0.0199266 + 2021-12-22 0.0102034 0.0220221 + 2021-12-23 0.0102077 0.0220114 + 2021-12-27 0.0102132 0.0213968 + 2021-12-28 0.0079822 0.0202206 + 2021-12-29 0.00799232 0.0199266 2021-12-30 0.00678748 0.0179653 - 2021-12-31 0.00534801 0.0116642 + 2021-12-31 0.00534796 0.0116642 126 rows omitted

Rolling regression with a window of 10

One of the common finance problems is to run a rolling window regression of firm returns over market returns. For doing this, we will use the lm() function from the GLM package. We will create a separate function regress() which would take in the data as an argument and use pre-defined strings to identify the returns columns, pass them to lm(), and return the results.

We start by downloading the S&P500 daily data from Yahoo Finance, then performing the same steps as above to come to a joined TSFrame object containing daily returns of S&P500 and IBM stock prices. Then, use rollapply() with bycolumn=false to tell rollapply() to pass in the entire TSFrame to the function in one go for each iteration within the window.

julia> sp500 = TSFrame(MarketData.yahoo("^GSPC"));
julia> sp500_adjclose = TSFrames.subset(sp500, date_from, date_to)[:, ["AdjClose"]]150×1 TSFrame with Date Index Index AdjClose Date Float64 @@ -278,4 +278,4 @@ sd::Real = Statistics.std(residuals(ll)) return (co, sd) endERROR: LoadError: UndefVarError: `@formula` not defined -in expression starting at REPL[6]:2
julia> rollapply(sp500_ibm_returns, regress, 10, bycolumn=false)ERROR: UndefVarError: `sp500_ibm_returns` not defined
+in expression starting at REPL[6]:2
julia> rollapply(sp500_ibm_returns, regress, 10, bycolumn=false)ERROR: UndefVarError: `sp500_ibm_returns` not defined diff --git a/dev/index.html b/dev/index.html index 730212c..11ca167 100644 --- a/dev/index.html +++ b/dev/index.html @@ -1,2 +1,2 @@ -Introduction · TSFrames.jl

TSFrames.jl

TSFrames provides a convenient interface for performing standard manipulations of timeseries data. The package uses DataFrame at it's core to allow powerful data manipulation functions while being lightweight. It is inspired by zoo and xts packages from the R world.

TSFrames wraps a familiar syntax for timeseries operations over DataFrame type, thereby, providing the user with full set of DataFrame functionalities as well. Integrations with other packages in the Julia ecosystem which are supported by DataFrames.jl come to TSFrames at little cost.

To start using TSFrames.jl take a look at the basic demo and then head to the User guide.

User guide

API reference

+Introduction · TSFrames.jl

TSFrames.jl

TSFrames provides a convenient interface for performing standard manipulations of timeseries data. The package uses DataFrame at it's core to allow powerful data manipulation functions while being lightweight. It is inspired by zoo and xts packages from the R world.

TSFrames wraps a familiar syntax for timeseries operations over DataFrame type, thereby, providing the user with full set of DataFrame functionalities as well. Integrations with other packages in the Julia ecosystem which are supported by DataFrames.jl come to TSFrames at little cost.

To start using TSFrames.jl take a look at the basic demo and then head to the User guide.

User guide

API reference

diff --git a/dev/search/index.html b/dev/search/index.html index 6c3f3c0..0b32924 100644 --- a/dev/search/index.html +++ b/dev/search/index.html @@ -1,2 +1,2 @@ -Search · TSFrames.jl

Loading search...

    +Search · TSFrames.jl

    Loading search...

      diff --git a/dev/ts-plot.svg b/dev/ts-plot.svg index 102d2b0..8f7f3c7 100644 --- a/dev/ts-plot.svg +++ b/dev/ts-plot.svg @@ -1,76 +1,76 @@ - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dev/user_guide/index.html b/dev/user_guide/index.html index 96d5b1b..87cf9f1 100644 --- a/dev/user_guide/index.html +++ b/dev/user_guide/index.html @@ -30,22 +30,22 @@ Index value Date Float64 ────────────────────── - 2007-01-01 0.628394 - 2007-01-02 9.91311 - 2007-01-03 4.37679 - 2007-01-04 5.68103 - 2007-01-05 7.76371 - 2007-01-06 9.60794 - 2007-01-07 2.45617 - 2007-01-08 1.44924 + 2007-01-01 0.387048 + 2007-01-02 1.1515 + 2007-01-03 0.828109 + 2007-01-04 4.2478 + 2007-01-05 4.30876 + 2007-01-06 2.93308 + 2007-01-07 6.29236 + 2007-01-08 6.90237 ⋮ ⋮ - 2008-02-29 2.15121 - 2008-03-01 2.61565 - 2008-03-02 1.68589 - 2008-03-03 9.1067 - 2008-03-04 4.47454 - 2008-03-05 6.98332 - 2008-03-06 2.27497 + 2008-02-29 7.89843 + 2008-03-01 2.87578 + 2008-03-02 4.7969 + 2008-03-03 8.40796 + 2008-03-04 3.32234 + 2008-03-05 0.977815 + 2008-03-06 0.491254 416 rows omitted

      In the above example you generate a random DataFrame and convert it into a TSFrame object ts. The top line of the ts object tells you the number of rows (431 here) and the number of columns (1) along with the Type of Index (Dates.Date in the above example).

      You can also fetch the number of rows and columns by using nr(ts), nc(ts), and size(ts) methods. Respectively, they fetch the number of rows, columns, and a Tuple of row and column numbers. A length(::TSFrame) method is also provided for convenience which returns the number of rows of it's argument.

      julia> nr(ts)431
      julia> nc(ts)1
      julia> size(ts)(431, 1)
      julia> length(ts)431

      Names of data columns can be fetched using the names(ts) method which returns a Vector{String} object. The Index column can be fetched as an object of Vector type by using the index(ts) method, it can also be fetched directly using the underlying coredata property of TSFrame: ts.coredata[!, :Index].

      julia> names(ts)1-element Vector{String}:
        "value"
      julia> index(ts)431-element Vector{Date}: 2007-01-01 @@ -71,480 +71,480 @@ Index value Date Float64 ────────────────────── - 2007-01-01 0.628394
      julia> ts[[3, 5], [1]] # third & fifth row, and first column2×1 TSFrame with Date Index + 2007-01-01 0.387048
      julia> ts[[3, 5], [1]] # third & fifth row, and first column2×1 TSFrame with Date Index Index value Date Float64 -───────────────────── - 2007-01-03 4.37679 - 2007-01-05 7.76371
      julia> ts[1:10, 1] # first 10 rows and the first column as a vector10-element Vector{Float64}: - 0.6283940583302283 - 9.91311321062652 - 4.376791196658348 - 5.681034837155801 - 7.763707667030197 - 9.607940488422514 - 2.4561685909232267 - 1.449240002613379 - 9.101391154870996 - 5.629503996179331
      julia> ts[1, [:value]] # using the column name1×1 TSFrame with Date Index +────────────────────── + 2007-01-03 0.828109 + 2007-01-05 4.30876
      julia> ts[1:10, 1] # first 10 rows and the first column as a vector10-element Vector{Float64}: + 0.38704751536183024 + 1.1515045758104459 + 0.8281087840899259 + 4.247799934622658 + 4.30876431094682 + 2.933078445718543 + 6.292362889695227 + 6.90236650635533 + 1.967008854162502 + 0.28452437132264863
      julia> ts[1, [:value]] # using the column name1×1 TSFrame with Date Index Index value Date Float64 ────────────────────── - 2007-01-01 0.628394

      Apart from integer-based row indexing and integer, name based column indexing, TSFrames provides special subsetting methods for date and time types defined inside the Dates module.

      julia> ts[Date(2007, 1, 10)] # on January 10, 20071×1 TSFrame with Date Index
      + 2007-01-01  0.387048

      Apart from integer-based row indexing and integer, name based column indexing, TSFrames provides special subsetting methods for date and time types defined inside the Dates module.

      julia> ts[Date(2007, 1, 10)] # on January 10, 20071×1 TSFrame with Date Index
        Index       value
        Date        Float64
      -─────────────────────
      - 2007-01-10   5.6295
      julia> ts[[Date(2007, 1, 10), Date(2007, 1, 11)]] # January 10, 112×1 TSFrame with Date Index +────────────────────── + 2007-01-10 0.284524
      julia> ts[[Date(2007, 1, 10), Date(2007, 1, 11)]] # January 10, 112×1 TSFrame with Date Index Index value Date Float64 -───────────────────── - 2007-01-10 5.6295 - 2007-01-11 4.09476
      julia> ts[Year(2007), Month(1)] # entire January 200731×1 TSFrame with Date Index +────────────────────── + 2007-01-10 0.284524 + 2007-01-11 8.65394
      julia> ts[Year(2007), Month(1)] # entire January 200731×1 TSFrame with Date Index Index value Date Float64 ─────────────────────── - 2007-01-01 0.628394 - 2007-01-02 9.91311 - 2007-01-03 4.37679 - 2007-01-04 5.68103 - 2007-01-05 7.76371 - 2007-01-06 9.60794 - 2007-01-07 2.45617 - 2007-01-08 1.44924 + 2007-01-01 0.387048 + 2007-01-02 1.1515 + 2007-01-03 0.828109 + 2007-01-04 4.2478 + 2007-01-05 4.30876 + 2007-01-06 2.93308 + 2007-01-07 6.29236 + 2007-01-08 6.90237 ⋮ ⋮ - 2007-01-25 7.62427 - 2007-01-26 4.75115 - 2007-01-27 7.77146 - 2007-01-28 1.71338 - 2007-01-29 5.74171 - 2007-01-30 1.44697 - 2007-01-31 1.88346 + 2007-01-25 3.59425 + 2007-01-26 1.98903 + 2007-01-27 3.77675 + 2007-01-28 5.18319 + 2007-01-29 3.6839 + 2007-01-30 1.47898 + 2007-01-31 8.58231 16 rows omitted
      julia> ts[Year(2007), Quarter(2)]91×1 TSFrame with Date Index Index value Date Float64 -────────────────────── - 2007-04-01 0.54749 - 2007-04-02 1.53644 - 2007-04-03 7.07105 - 2007-04-04 1.72242 - 2007-04-05 7.22042 - 2007-04-06 4.66166 - 2007-04-07 4.44036 - 2007-04-08 6.45644 - ⋮ ⋮ - 2007-06-24 7.61045 - 2007-06-25 7.6245 - 2007-06-26 8.30134 - 2007-06-27 7.93709 - 2007-06-28 8.14424 - 2007-06-29 1.129 - 2007-06-30 6.05185 - 76 rows omitted

      Finally, one can also use the dot notation to get a column as a vector.

      julia> ts.value # get the value column as a vector431-element Vector{Float64}:
      - 0.6283940583302283
      - 9.91311321062652
      - 4.376791196658348
      - 5.681034837155801
      - 7.763707667030197
      - 9.607940488422514
      - 2.4561685909232267
      - 1.449240002613379
      - 9.101391154870996
      - 5.629503996179331
      +───────────────────────
      + 2007-04-01  9.85869
      + 2007-04-02  1.07482
      + 2007-04-03  9.03223
      + 2007-04-04  7.22205
      + 2007-04-05  3.37678
      + 2007-04-06  3.19926
      + 2007-04-07  6.34568
      + 2007-04-08  7.0705
      +     ⋮           ⋮
      + 2007-06-24  6.47057
      + 2007-06-25  8.98154
      + 2007-06-26  6.72291
      + 2007-06-27  8.20424
      + 2007-06-28  9.79371
      + 2007-06-29  8.12382
      + 2007-06-30  8.30455
      +        76 rows omitted

      Finally, one can also use the dot notation to get a column as a vector.

      julia> ts.value # get the value column as a vector431-element Vector{Float64}:
      + 0.38704751536183024
      + 1.1515045758104459
      + 0.8281087840899259
      + 4.247799934622658
      + 4.30876431094682
      + 2.933078445718543
      + 6.292362889695227
      + 6.90236650635533
      + 1.967008854162502
      + 0.28452437132264863
        ⋮
      - 3.912855299019582
      - 2.607310760636751
      - 2.1512143208816283
      - 2.615654160980798
      - 1.6858891621992256
      - 9.106703691553378
      - 4.474538013882043
      - 6.983319726891276
      - 2.274972748115381

      Summary statistics

      The describe() method prints summary statistics of the TSFrame object. The output is a DataFrame which includes the number of missing values, data types of columns along with computed statistical values.

      julia> TSFrames.describe(ts)2×7 DataFrame
      + 5.104877413986378
      + 9.129228844236119
      + 7.89843168083289
      + 2.875775562463223
      + 4.796904761826152
      + 8.40795630570721
      + 3.322340451987503
      + 0.9778154431116803
      + 0.4912539268878202

      Summary statistics

      The describe() method prints summary statistics of the TSFrame object. The output is a DataFrame which includes the number of missing values, data types of columns along with computed statistical values.

      julia> TSFrames.describe(ts)2×7 DataFrame
        Row │ variable  mean     min         median      max         nmissing  eltype ⋯
            │ Symbol    Union…   Any         Any         Any         Int64     DataTy ⋯
       ─────┼──────────────────────────────────────────────────────────────────────────
          1 │ Index              2007-01-01  2007-08-04  2008-03-06         0  Date   ⋯
      -   2 │ value     4.90471  0.0121142   4.94807     9.99342            0  Float6
      +   2 │ value     4.80712  0.00908461  4.64683     9.97729            0  Float6
                                                                       1 column omitted

      Plotting

      A TSFrame object can be plotted using the plot() function of the Plots package. The plotting functionality is provided by RecipesBase package so all the flexibility and functionality of the Plots package is available for users.

      using Plots
       plot(ts, size=(600,400); legend=false)
      - + - + - + - + - + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + +

      Applying a function over a period

      The apply method allows you to aggregate the TSFrame object over a period type (Dates.Period(@ref)) and return the output of applying the function on each period. For example, to convert frequency of daily timeseries to monthly you may use first(), last(), or Statistics.mean() functions and the period as Dates.Month.

      julia> using Statistics
      julia> ts_monthly = apply(ts, Month(1), last) # convert to monthly series using the last value for each month15×1 TSFrame with Date Index Index value_last Date Float64 ──────────────────────── - 2007-01-01 1.88346 - 2007-02-01 0.0243763 - 2007-03-01 4.35609 - 2007-04-01 4.12234 - 2007-05-01 8.00231 - 2007-06-01 6.05185 - 2007-07-01 1.56668 - 2007-08-01 0.552275 - 2007-09-01 4.89281 - 2007-10-01 9.57445 - 2007-11-01 4.25986 - 2007-12-01 1.74452 - 2008-01-01 1.52569 - 2008-02-01 2.15121 - 2008-03-01 2.27497
      julia> ts_weekly = apply(ts, Week(1), Statistics.std) # compute weekly standard deviation62×1 TSFrame with Date Index + 2007-01-01 8.58231 + 2007-02-01 5.43155 + 2007-03-01 9.72411 + 2007-04-01 5.56651 + 2007-05-01 8.27571 + 2007-06-01 8.30455 + 2007-07-01 7.43596 + 2007-08-01 9.29006 + 2007-09-01 7.34917 + 2007-10-01 8.43661 + 2007-11-01 4.81665 + 2007-12-01 7.29296 + 2008-01-01 5.89334 + 2008-02-01 7.89843 + 2008-03-01 0.491254
      julia> ts_weekly = apply(ts, Week(1), Statistics.std) # compute weekly standard deviation62×1 TSFrame with Date Index Index value_std Date Float64 ─────────────────────── - 2007-01-01 3.5411 - 2007-01-08 3.31893 - 2007-01-15 3.83519 - 2007-01-22 3.43321 - 2007-01-29 3.0437 - 2007-02-05 3.11448 - 2007-02-12 3.42289 - 2007-02-19 3.32921 + 2007-01-01 2.19742 + 2007-01-08 2.89555 + 2007-01-15 3.75153 + 2007-01-22 2.36153 + 2007-01-29 2.63724 + 2007-02-05 2.57961 + 2007-02-12 2.26862 + 2007-02-19 2.22174 ⋮ ⋮ - 2008-01-21 3.04734 - 2008-01-28 3.02883 - 2008-02-04 1.78777 - 2008-02-11 3.02604 - 2008-02-18 2.75477 - 2008-02-25 2.79925 - 2008-03-03 2.97124 + 2008-01-21 2.28745 + 2008-01-28 2.31048 + 2008-02-04 3.18292 + 2008-02-11 3.02602 + 2008-02-18 2.69851 + 2008-02-25 3.38693 + 2008-03-03 3.62277 47 rows omitted
      julia> apply(ts, Week(1), Statistics.std, last) # same as above but index contains the last date of the week62×1 TSFrame with Date Index Index value_std Date Float64 ─────────────────────── - 2007-01-07 3.5411 - 2007-01-14 3.31893 - 2007-01-21 3.83519 - 2007-01-28 3.43321 - 2007-02-04 3.0437 - 2007-02-11 3.11448 - 2007-02-18 3.42289 - 2007-02-25 3.32921 + 2007-01-07 2.19742 + 2007-01-14 2.89555 + 2007-01-21 3.75153 + 2007-01-28 2.36153 + 2007-02-04 2.63724 + 2007-02-11 2.57961 + 2007-02-18 2.26862 + 2007-02-25 2.22174 ⋮ ⋮ - 2008-01-27 3.04734 - 2008-02-03 3.02883 - 2008-02-10 1.78777 - 2008-02-17 3.02604 - 2008-02-24 2.75477 - 2008-03-02 2.79925 - 2008-03-06 2.97124 + 2008-01-27 2.28745 + 2008-02-03 2.31048 + 2008-02-10 3.18292 + 2008-02-17 3.02602 + 2008-02-24 2.69851 + 2008-03-02 3.38693 + 2008-03-06 3.62277 47 rows omitted
      julia> apply(ts, Week(1), Statistics.std, last, renamecols=false) # do not rename column62×1 TSFrame with Date Index Index value Date Float64 ───────────────────── - 2007-01-07 3.5411 - 2007-01-14 3.31893 - 2007-01-21 3.83519 - 2007-01-28 3.43321 - 2007-02-04 3.0437 - 2007-02-11 3.11448 - 2007-02-18 3.42289 - 2007-02-25 3.32921 + 2007-01-07 2.19742 + 2007-01-14 2.89555 + 2007-01-21 3.75153 + 2007-01-28 2.36153 + 2007-02-04 2.63724 + 2007-02-11 2.57961 + 2007-02-18 2.26862 + 2007-02-25 2.22174 ⋮ ⋮ - 2008-01-27 3.04734 - 2008-02-03 3.02883 - 2008-02-10 1.78777 - 2008-02-17 3.02604 - 2008-02-24 2.75477 - 2008-03-02 2.79925 - 2008-03-06 2.97124 + 2008-01-27 2.28745 + 2008-02-03 2.31048 + 2008-02-10 3.18292 + 2008-02-17 3.02602 + 2008-02-24 2.69851 + 2008-03-02 3.38693 + 2008-03-06 3.62277 47 rows omitted

      Joins: Row and column binding with other objects

      TSFrames provides methods to join two TSFrame objects by columns: join (alias: cbind) or by rows: vcat (alias: rbind). Both the methods provide some basic intelligence while doing the merge.

      join merges two datasets based on the Index values of both objects. Depending on the join strategy employed the final object may only contain index values only from the left object (using jointype=:JoinLeft), the right object (using jointype=:JoinRight), intersection of both objects (using jointype=:JoinBoth), or a union of both objects (jointype=:JoinAll) while inserting missing values where index values are missing from any of the other object.

      julia> dates = collect(Date(2007,1,1):Day(1):Date(2007,1,30));
      julia> ts2 = TSFrame(rand(length(dates)), dates)30×1 TSFrame with Date Index Index x1 Date Float64 -──────────────────────── - 2007-01-01 0.0777402 - 2007-01-02 0.479318 - 2007-01-03 0.0174322 - 2007-01-04 0.285702 - 2007-01-05 0.782804 - 2007-01-06 0.550327 - 2007-01-07 0.470478 - 2007-01-08 0.184691 +─────────────────────── + 2007-01-01 0.163387 + 2007-01-02 0.912665 + 2007-01-03 0.387569 + 2007-01-04 0.956656 + 2007-01-05 0.553296 + 2007-01-06 0.352498 + 2007-01-07 0.400976 + 2007-01-08 0.337264 ⋮ ⋮ - 2007-01-24 0.0836173 - 2007-01-25 0.0647352 - 2007-01-26 0.417239 - 2007-01-27 0.150214 - 2007-01-28 0.041159 - 2007-01-29 0.575969 - 2007-01-30 0.782602 - 15 rows omitted
      julia> join(ts, ts2; jointype=:JoinAll) # cbind/join on Index column431×2 TSFrame with Date Index + 2007-01-24 0.498155 + 2007-01-25 0.897508 + 2007-01-26 0.9823 + 2007-01-27 0.137462 + 2007-01-28 0.515626 + 2007-01-29 0.9231 + 2007-01-30 0.974959 + 15 rows omitted
      julia> join(ts, ts2; jointype=:JoinAll) # cbind/join on Index column431×2 TSFrame with Date Index Index value x1 Date Float64? Float64? ─────────────────────────────────────── - 2007-01-01 0.628394 0.0777402 - 2007-01-02 9.91311 0.479318 - 2007-01-03 4.37679 0.0174322 - 2007-01-04 5.68103 0.285702 - 2007-01-05 7.76371 0.782804 - 2007-01-06 9.60794 0.550327 - 2007-01-07 2.45617 0.470478 - 2007-01-08 1.44924 0.184691 + 2007-01-01 0.387048 0.163387 + 2007-01-02 1.1515 0.912665 + 2007-01-03 0.828109 0.387569 + 2007-01-04 4.2478 0.956656 + 2007-01-05 4.30876 0.553296 + 2007-01-06 2.93308 0.352498 + 2007-01-07 6.29236 0.400976 + 2007-01-08 6.90237 0.337264 ⋮ ⋮ ⋮ - 2008-02-29 2.15121 missing - 2008-03-01 2.61565 missing - 2008-03-02 1.68589 missing - 2008-03-03 9.1067 missing - 2008-03-04 4.47454 missing - 2008-03-05 6.98332 missing - 2008-03-06 2.27497 missing + 2008-02-29 7.89843 missing + 2008-03-01 2.87578 missing + 2008-03-02 4.7969 missing + 2008-03-03 8.40796 missing + 2008-03-04 3.32234 missing + 2008-03-05 0.977815 missing + 2008-03-06 0.491254 missing 416 rows omitted

      vcat also works similarly but merges two datasets by rows. This method also uses certain strategies provided via colmerge argument to check for certain conditions before doing the merge, throwing an error if the conditions are not satisfied.

      colmerge can be passed setequal which merges only if both objects have same column names, orderequal which merges only if both objects have same column names and columns are in the same order, intersect merges only the columns which are common to both objects, and union which merges even if the columns differ between the two objects, the resulting object has the columns filled with missing, if necessary.

      For vcat, if the values of Index are same in the two objects then all the index values along with values in other columns are kept in the resulting object. So, a vcat operation may result in duplicate Index values and the results from other operations may differ or even throw unknown errors.

      julia> dates = collect(Date(2008,4,1):Day(1):Date(2008,4,30));
      julia> ts3 = TSFrame(DataFrame(values=rand(length(dates)), Index=dates))30×1 TSFrame with Date Index Index values Date Float64 ─────────────────────── - 2008-04-01 0.461257 - 2008-04-02 0.350668 - 2008-04-03 0.476737 - 2008-04-04 0.930412 - 2008-04-05 0.487752 - 2008-04-06 0.854267 - 2008-04-07 0.0423403 - 2008-04-08 0.151409 + 2008-04-01 0.264262 + 2008-04-02 0.799757 + 2008-04-03 0.228485 + 2008-04-04 0.536086 + 2008-04-05 0.896222 + 2008-04-06 0.941203 + 2008-04-07 0.893276 + 2008-04-08 0.151341 ⋮ ⋮ - 2008-04-24 0.703647 - 2008-04-25 0.90018 - 2008-04-26 0.666877 - 2008-04-27 0.732916 - 2008-04-28 0.0350552 - 2008-04-29 0.056777 - 2008-04-30 0.655176 + 2008-04-24 0.849827 + 2008-04-25 0.799659 + 2008-04-26 0.103818 + 2008-04-27 0.0153157 + 2008-04-28 0.0130436 + 2008-04-29 0.328759 + 2008-04-30 0.859211 15 rows omitted
      julia> vcat(ts, ts3) # do the merge461×2 TSFrame with Date Index Index value values Date Float64? Float64? ───────────────────────────────────────────── - 2007-01-01 0.628394 missing - 2007-01-02 9.91311 missing - 2007-01-03 4.37679 missing - 2007-01-04 5.68103 missing - 2007-01-05 7.76371 missing - 2007-01-06 9.60794 missing - 2007-01-07 2.45617 missing - 2007-01-08 1.44924 missing + 2007-01-01 0.387048 missing + 2007-01-02 1.1515 missing + 2007-01-03 0.828109 missing + 2007-01-04 4.2478 missing + 2007-01-05 4.30876 missing + 2007-01-06 2.93308 missing + 2007-01-07 6.29236 missing + 2007-01-08 6.90237 missing ⋮ ⋮ ⋮ - 2008-04-24 missing 0.703647 - 2008-04-25 missing 0.90018 - 2008-04-26 missing 0.666877 - 2008-04-27 missing 0.732916 - 2008-04-28 missing 0.0350552 - 2008-04-29 missing 0.056777 - 2008-04-30 missing 0.655176 + 2008-04-24 missing 0.849827 + 2008-04-25 missing 0.799659 + 2008-04-26 missing 0.103818 + 2008-04-27 missing 0.0153157 + 2008-04-28 missing 0.0130436 + 2008-04-29 missing 0.328759 + 2008-04-30 missing 0.859211 446 rows omitted

      Rolling window operations

      The rollapply applies a function over a fixed-size rolling window on the dataset. In the example below, we compute the 10-day average of dataset values on a rolling basis.

      julia> rollapply(ts, mean, 10)422×1 TSFrame with Date Index
        Index       rolling_value_mean
        Date        Float64
       ────────────────────────────────
      - 2007-01-10             5.66073
      - 2007-01-11             6.00737
      - 2007-01-12             5.95108
      - 2007-01-13             5.61075
      - 2007-01-14             5.61826
      - 2007-01-15             4.87269
      - 2007-01-16             4.3863
      - 2007-01-17             4.22547
      + 2007-01-10             2.93026
      + 2007-01-11             3.75695
      + 2007-01-12             4.11437
      + 2007-01-13             4.43992
      + 2007-01-14             4.64245
      + 2007-01-15             4.60143
      + 2007-01-16             5.25857
      + 2007-01-17             4.76573
            ⋮               ⋮
      - 2008-02-29             3.77309
      - 2008-03-01             3.9594
      - 2008-03-02             4.10473
      - 2008-03-03             4.78991
      - 2008-03-04             5.08711
      - 2008-03-05             4.95339
      - 2008-03-06             4.26836
      + 2008-02-29             4.54785
      + 2008-03-01             4.63275
      + 2008-03-02             4.51709
      + 2008-03-03             5.17973
      + 2008-03-04             5.49709
      + 2008-03-05             5.26184
      + 2008-03-06             5.26533
                       407 rows omitted

      Computing rolling difference and percent change

      Similar to apply and rollapply there are specific methods to compute rolling differences and percent changes of a TSFrame object. The diff method computes mathematical difference of values in adjacent rows, inserting missing in the first row. pctchange computes the percentage change between adjacent rows.

      julia> diff(ts)431×1 TSFrame with Date Index
        Index       value
        Date        Float64?
      -────────────────────────────
      +─────────────────────────────
        2007-01-01  missing
      - 2007-01-02        9.28472
      - 2007-01-03       -5.53632
      - 2007-01-04        1.30424
      - 2007-01-05        2.08267
      - 2007-01-06        1.84423
      - 2007-01-07       -7.15177
      - 2007-01-08       -1.00693
      -     ⋮             ⋮
      - 2008-02-29       -0.456096
      - 2008-03-01        0.46444
      - 2008-03-02       -0.929765
      - 2008-03-03        7.42081
      - 2008-03-04       -4.63217
      - 2008-03-05        2.50878
      - 2008-03-06       -4.70835
      -            416 rows omitted
      julia> pctchange(ts)431×1 TSFrame with Date Index + 2007-01-02 0.764457 + 2007-01-03 -0.323396 + 2007-01-04 3.41969 + 2007-01-05 0.0609644 + 2007-01-06 -1.37569 + 2007-01-07 3.35928 + 2007-01-08 0.610004 + ⋮ ⋮ + 2008-02-29 -1.2308 + 2008-03-01 -5.02266 + 2008-03-02 1.92113 + 2008-03-03 3.61105 + 2008-03-04 -5.08562 + 2008-03-05 -2.34453 + 2008-03-06 -0.486562 + 416 rows omitted
      julia> pctchange(ts)431×1 TSFrame with Date Index Index value Date Float64? ───────────────────────────── 2007-01-01 missing - 2007-01-02 14.7753 - 2007-01-03 -0.558485 - 2007-01-04 0.297991 - 2007-01-05 0.366601 - 2007-01-06 0.237545 - 2007-01-07 -0.744361 - 2007-01-08 -0.409959 + 2007-01-02 1.9751 + 2007-01-03 -0.280846 + 2007-01-04 4.12952 + 2007-01-05 0.014352 + 2007-01-06 -0.319276 + 2007-01-07 1.14531 + 2007-01-08 0.0969435 ⋮ ⋮ - 2008-02-29 -0.17493 - 2008-03-01 0.215897 - 2008-03-02 -0.355462 - 2008-03-03 4.40172 - 2008-03-04 -0.508654 - 2008-03-05 0.560679 - 2008-03-06 -0.674228 + 2008-02-29 -0.134819 + 2008-03-01 -0.635905 + 2008-03-02 0.668039 + 2008-03-03 0.752788 + 2008-03-04 -0.604858 + 2008-03-05 -0.705685 + 2008-03-06 -0.497601 416 rows omitted

      Computing log of data values

      julia> log.(ts)431×1 TSFrame with Date Index
        Index       value_log
        Date        Float64
      -───────────────────────
      - 2007-01-01  -0.464588
      - 2007-01-02   2.29386
      - 2007-01-03   1.47632
      - 2007-01-04   1.73713
      - 2007-01-05   2.04946
      - 2007-01-06   2.26259
      - 2007-01-07   0.898603
      - 2007-01-08   0.371039
      +────────────────────────
      + 2007-01-01  -0.949208
      + 2007-01-02   0.141069
      + 2007-01-03  -0.188611
      + 2007-01-04   1.4464
      + 2007-01-05   1.46065
      + 2007-01-06   1.07605
      + 2007-01-07   1.83934
      + 2007-01-08   1.93186
            ⋮           ⋮
      - 2008-02-29   0.766032
      - 2008-03-01   0.961514
      - 2008-03-02   0.522293
      - 2008-03-03   2.20901
      - 2008-03-04   1.4984
      - 2008-03-05   1.94352
      - 2008-03-06   0.821968
      -       416 rows omitted

      Creating lagged/leading series

      lag() and lead() provide ways to lag or lead a series respectively by a fixed value, inserting missing where required.

      julia> lag(ts, 2)431×1 TSFrame with Date Index
      + 2008-02-29   2.06666
      + 2008-03-01   1.05632
      + 2008-03-02   1.56797
      + 2008-03-03   2.12918
      + 2008-03-04   1.20067
      + 2008-03-05  -0.0224343
      + 2008-03-06  -0.710794
      +        416 rows omitted

      Creating lagged/leading series

      lag() and lead() provide ways to lag or lead a series respectively by a fixed value, inserting missing where required.

      julia> lag(ts, 2)431×1 TSFrame with Date Index
        Index       value
        Date        Float64?
       ────────────────────────────
        2007-01-01  missing
        2007-01-02  missing
      - 2007-01-03        0.628394
      - 2007-01-04        9.91311
      - 2007-01-05        4.37679
      - 2007-01-06        5.68103
      - 2007-01-07        7.76371
      - 2007-01-08        9.60794
      + 2007-01-03        0.387048
      + 2007-01-04        1.1515
      + 2007-01-05        0.828109
      + 2007-01-06        4.2478
      + 2007-01-07        4.30876
      + 2007-01-08        2.93308
            ⋮             ⋮
      - 2008-02-29        3.91286
      - 2008-03-01        2.60731
      - 2008-03-02        2.15121
      - 2008-03-03        2.61565
      - 2008-03-04        1.68589
      - 2008-03-05        9.1067
      - 2008-03-06        4.47454
      + 2008-02-29        5.10488
      + 2008-03-01        9.12923
      + 2008-03-02        7.89843
      + 2008-03-03        2.87578
      + 2008-03-04        4.7969
      + 2008-03-05        8.40796
      + 2008-03-06        3.32234
                   416 rows omitted
      julia> lead(ts, 2)431×1 TSFrame with Date Index Index value Date Float64? ──────────────────────────── - 2007-01-01 4.37679 - 2007-01-02 5.68103 - 2007-01-03 7.76371 - 2007-01-04 9.60794 - 2007-01-05 2.45617 - 2007-01-06 1.44924 - 2007-01-07 9.10139 - 2007-01-08 5.6295 + 2007-01-01 0.828109 + 2007-01-02 4.2478 + 2007-01-03 4.30876 + 2007-01-04 2.93308 + 2007-01-05 6.29236 + 2007-01-06 6.90237 + 2007-01-07 1.96701 + 2007-01-08 0.284524 ⋮ ⋮ - 2008-02-29 1.68589 - 2008-03-01 9.1067 - 2008-03-02 4.47454 - 2008-03-03 6.98332 - 2008-03-04 2.27497 + 2008-02-29 4.7969 + 2008-03-01 8.40796 + 2008-03-02 3.32234 + 2008-03-03 0.977815 + 2008-03-04 0.491254 2008-03-05 missing 2008-03-06 missing 416 rows omitted

      Converting to Matrix and DataFrame

      You can easily convert a TSFrame object into a Matrix or fetch the DataFrame for doing operations which are outside of the TSFrames scope.

      julia> ts[:, 1] # convert column 1 to a vector of floats431-element Vector{Float64}:
      - 0.6283940583302283
      - 9.91311321062652
      - 4.376791196658348
      - 5.681034837155801
      - 7.763707667030197
      - 9.607940488422514
      - 2.4561685909232267
      - 1.449240002613379
      - 9.101391154870996
      - 5.629503996179331
      + 0.38704751536183024
      + 1.1515045758104459
      + 0.8281087840899259
      + 4.247799934622658
      + 4.30876431094682
      + 2.933078445718543
      + 6.292362889695227
      + 6.90236650635533
      + 1.967008854162502
      + 0.28452437132264863
        ⋮
      - 3.912855299019582
      - 2.607310760636751
      - 2.1512143208816283
      - 2.615654160980798
      - 1.6858891621992256
      - 9.106703691553378
      - 4.474538013882043
      - 6.983319726891276
      - 2.274972748115381
      julia> Matrix(ts) # convert entire TSFrame into a Matrix431×1 Matrix{Float64}: - 0.6283940583302283 - 9.91311321062652 - 4.376791196658348 - 5.681034837155801 - 7.763707667030197 - 9.607940488422514 - 2.4561685909232267 - 1.449240002613379 - 9.101391154870996 - 5.629503996179331 + 5.104877413986378 + 9.129228844236119 + 7.89843168083289 + 2.875775562463223 + 4.796904761826152 + 8.40795630570721 + 3.322340451987503 + 0.9778154431116803 + 0.4912539268878202
      julia> Matrix(ts) # convert entire TSFrame into a Matrix431×1 Matrix{Float64}: + 0.38704751536183024 + 1.1515045758104459 + 0.8281087840899259 + 4.247799934622658 + 4.30876431094682 + 2.933078445718543 + 6.292362889695227 + 6.90236650635533 + 1.967008854162502 + 0.28452437132264863 ⋮ - 3.912855299019582 - 2.607310760636751 - 2.1512143208816283 - 2.615654160980798 - 1.6858891621992256 - 9.106703691553378 - 4.474538013882043 - 6.983319726891276 - 2.274972748115381
      julia> select(ts.coredata, :Index, :value, DataFrames.nrow) # use the underlying DataFrame for other operations431×3 DataFrame + 5.104877413986378 + 9.129228844236119 + 7.89843168083289 + 2.875775562463223 + 4.796904761826152 + 8.40795630570721 + 3.322340451987503 + 0.9778154431116803 + 0.4912539268878202
      julia> select(ts.coredata, :Index, :value, DataFrames.nrow) # use the underlying DataFrame for other operations431×3 DataFrame Row │ Index value nrow │ Date Float64 Int64 ─────┼───────────────────────────── - 1 │ 2007-01-01 0.628394 431 - 2 │ 2007-01-02 9.91311 431 - 3 │ 2007-01-03 4.37679 431 - 4 │ 2007-01-04 5.68103 431 - 5 │ 2007-01-05 7.76371 431 - 6 │ 2007-01-06 9.60794 431 - 7 │ 2007-01-07 2.45617 431 - 8 │ 2007-01-08 1.44924 431 + 1 │ 2007-01-01 0.387048 431 + 2 │ 2007-01-02 1.1515 431 + 3 │ 2007-01-03 0.828109 431 + 4 │ 2007-01-04 4.2478 431 + 5 │ 2007-01-05 4.30876 431 + 6 │ 2007-01-06 2.93308 431 + 7 │ 2007-01-07 6.29236 431 + 8 │ 2007-01-08 6.90237 431 ⋮ │ ⋮ ⋮ ⋮ - 425 │ 2008-02-29 2.15121 431 - 426 │ 2008-03-01 2.61565 431 - 427 │ 2008-03-02 1.68589 431 - 428 │ 2008-03-03 9.1067 431 - 429 │ 2008-03-04 4.47454 431 - 430 │ 2008-03-05 6.98332 431 - 431 │ 2008-03-06 2.27497 431 + 425 │ 2008-02-29 7.89843 431 + 426 │ 2008-03-01 2.87578 431 + 427 │ 2008-03-02 4.7969 431 + 428 │ 2008-03-03 8.40796 431 + 429 │ 2008-03-04 3.32234 431 + 430 │ 2008-03-05 0.977815 431 + 431 │ 2008-03-06 0.491254 431 416 rows omitted

      Writing TSFrame into a CSV file

      Writing a TSFrame object into a CSV file can be done easily by using the underlying coredata property. This DataFrame can be passed to the CSV.write method for writing into a file.

      julia> CSV.write("/tmp/demo_ts.csv", ts)"/tmp/demo_ts.csv"

      Broadcasting

      Broadcasting can be used on a TSFrame object to apply a function to a subset of it's columns.

      julia> using TSFrames, DataFrames;
       
       julia> ts = TSFrame(DataFrame(Index = [1, 2, 3, 4, 5], A = [10.1, 12.4, 42.4, 24.1, 242.5], B = [2, 4, 6, 8, 10]))
      @@ -623,4 +623,4 @@ 

      Note

      This discussion warrants a note about how we've implemented the Tables.jl interfaces. Since TSFrame objects are nothing but a wrapper around a DataFrame, our implementations of these interfaces just call DataFrames.jl's implementations. Moreover, while constructing TSFrame objects out of other Tables.jl compatible types, our constructor first converts the input table to a DataFrame, and then converts the DataFrame to a TSFrame object.

      +julia> CSV.read("ts.csv", TSFrame);

      Note

      This discussion warrants a note about how we've implemented the Tables.jl interfaces. Since TSFrame objects are nothing but a wrapper around a DataFrame, our implementations of these interfaces just call DataFrames.jl's implementations. Moreover, while constructing TSFrame objects out of other Tables.jl compatible types, our constructor first converts the input table to a DataFrame, and then converts the DataFrame to a TSFrame object.