TSFrames API reference

API reference of TSFrames.

Base.MatrixMethod

Conversion of non-Index data to Matrix

Data in non-index columns of a TSFrame object can be converted into a Matrix type for further numerical analysis using the Matrix() constructor.

Examples

julia> using Random;
julia> random(x) = rand(MersenneTwister(123), x);
julia> ts = TSFrame([random(10) random(10)])
julia> show(ts)
(10 x 2) TSFrame with Int64 Index

 Index  x1         x2
 Int64  Float64    Float64
─────────────────────────────
     1  0.768448   0.768448
     2  0.940515   0.940515
     3  0.673959   0.673959
     4  0.395453   0.395453
     5  0.313244   0.313244
     6  0.662555   0.662555
     7  0.586022   0.586022
     8  0.0521332  0.0521332
     9  0.26864    0.26864
    10  0.108871   0.108871

julia> Matrix(ts)
10×2 Matrix{Float64}:
 0.768448   0.768448
 0.940515   0.940515
 0.673959   0.673959
 0.395453   0.395453
 0.313244   0.313244
 0.662555   0.662555
 0.586022   0.586022
 0.0521332  0.0521332
 0.26864    0.26864
 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 first argument can accept a DataFrame, Vector, or an Array with 2-dimensions. Any Tables.jl compatible table is also accepted as an input.

If the first argument is a DataFrame then the DataFrame may contain an indexing column also. The indexing column can be identified by providing either the column number or the column name as String or Symbol in the DataFrame as the second argument index.

In case, an indexing column is not present in the DataFrame then a sequential index is created automatically.

The input DataFrame is sorted during construction and is stored in the property coredata. The index is stored in the column called Index of the coredata DataFrame. This index can be fetched using the index() method.

Providing an empty DataFrame to the constructor results in a TSFrame object with an empty index and no columns. Similarly, providing empty Vector or empty Array as the first argument results in an empty TSFrame with no columns.

Permitted data inputs to the constructors are DataFrame, Vector, and 2-dimensional Array.

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}}
TSFrame(coredata::DataFrame; issorted = false, copycols = true)
TSFrame(coredata::DataFrame, index::UnitRange{Int}; issorted = false, copycols = true)
TSFrame(coredata::AbstractVector{T}, index::AbstractVector{V}; colnames=:auto, issorted = false, copycols = true) where {T, V}
TSFrame(coredata::AbstractVector{T}; colnames=:auto, issorted = false, copycols = true) where {T}
TSFrame(coredata::AbstractArray{T,2}; colnames=:auto, issorted = false, copycols = true) where {T}
TSFrame(coredata::AbstractArray{T,2}, index::AbstractVector{V}; colnames=:auto, issorted = false, copycols = true) where {T, V}
TSFrame(IndexType::DataType; n::Int=1)
TSFrame(IndexType::DataType, cols::Vector{Tuple{DataType, S}}; issorted = false, copycols = true) where S <: Union{Symbol, String}

When issorted is true, no sort operations are performed on the input. Whencopycolsistrue, the inputs are not copied, but placed directly in the TSFrame. This can be dangerous, so please only set this tofalse` if the input arrays will not be mutated later.

issorted = true, copycols = false offers performance benefits, especially when constructing TSFrames in loops or other performance sensitive code.

Examples

julia> using Random;
julia> random(x) = rand(MersenneTwister(123), x);

julia> df = DataFrame(x1 = random(10))
10×1 DataFrame
 Row │ x1
     │ Float64
─────┼───────────
   1 │ 0.768448
   2 │ 0.940515
   3 │ 0.673959
   4 │ 0.395453
   5 │ 0.313244
   6 │ 0.662555
   7 │ 0.586022
   8 │ 0.0521332
   9 │ 0.26864
  10 │ 0.108871

julia> ts = TSFrame(df)   # generates index
(10 x 1) TSFrame with Int64 Index

 Index  x1
 Int64  Float64
──────────────────
     1  0.768448
     2  0.940515
     3  0.673959
     4  0.395453
     5  0.313244
     6  0.662555
     7  0.586022
     8  0.0521332
     9  0.26864
    10  0.108871

# ts.coredata is a DataFrame
julia> combine(ts.coredata, :x1 => Statistics.mean, DataFrames.nrow)
1×2 DataFrame
 Row │ x1_mean  nrow
     │ Float64  Int64
─────┼────────────────
   1 │ 0.49898    418

julia> df = DataFrame(ind = [1, 2, 3], x1 = random(3))
3×2 DataFrame
 Row │ ind    x1
     │ Int64  Float64
─────┼─────────────────
   1 │     1  0.768448
   2 │     2  0.940515
   3 │     3  0.673959

julia> ts = TSFrame(df, 1)        # the first column is index
(3 x 1) TSFrame with Int64 Index

 Index  x1
 Int64  Float64
─────────────────
     1  0.768448
     2  0.940515
     3  0.673959

julia> df = DataFrame(x1 = random(3), x2 = random(3), Index = [1, 2, 3]);
3×3 DataFrame
 Row │ x1        x2        Index
     │ Float64   Float64   Int64
─────┼───────────────────────────
   1 │ 0.768448  0.768448      1
   2 │ 0.940515  0.940515      2
   3 │ 0.673959  0.673959      3

julia> ts = TSFrame(df)   # uses existing `Index` column
(3 x 2) TSFrame with Int64 Index

 Index  x1        x2
 Int64  Float64   Float64
───────────────────────────
     1  0.768448  0.768448
     2  0.940515  0.940515
     3  0.673959  0.673959

julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10));

julia> df = DataFrame(dates = dates, x1 = random(10))
10×2 DataFrame
 Row │ dates       x1
     │ Date        Float64
─────┼───────────────────────
   1 │ 2017-01-01  0.768448
   2 │ 2017-01-02  0.940515
   3 │ 2017-01-03  0.673959
   4 │ 2017-01-04  0.395453
   5 │ 2017-01-05  0.313244
   6 │ 2017-01-06  0.662555
   7 │ 2017-01-07  0.586022
   8 │ 2017-01-08  0.0521332
   9 │ 2017-01-09  0.26864
  10 │ 2017-01-10  0.108871

julia> ts = TSFrame(df, :dates)
(10 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871

julia> ts = TSFrame(DataFrame(x1=random(10)), dates);

julia> ts = TSFrame(random(10))
(10 x 1) TSFrame with Int64 Index

 Index  x1
 Int64  Float64
──────────────────
     1  0.768448
     2  0.940515
     3  0.673959
     4  0.395453
     5  0.313244
     6  0.662555
     7  0.586022
     8  0.0521332
     9  0.26864
    10  0.108871

julia> ts = TSFrame(random(10), colnames=[:A]) # column is named A
(10 x 1) TSFrame with Int64 Index

 Index  A
 Int64  Float64
──────────────────
     1  0.768448
     2  0.940515
     3  0.673959
     4  0.395453
     5  0.313244
     6  0.662555
     7  0.586022
     8  0.0521332
     9  0.26864
    10  0.108871

julia> ts = TSFrame(random(10), dates)
(10 x 1) TSFrame with Date Index

 Index       x1        
 Date        Float64   
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871

julia> ts = TSFrame(random(10), dates, colnames=[:A]) # column is named A
(10 x 1) TSFrame with Date Index

 Index       A         
 Date        Float64   
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871

julia> ts = TSFrame([random(10) random(10)]) # matrix object
(10 x 2) TSFrame with Int64 Index

 Index  x1         x2        
 Int64  Float64    Float64   
─────────────────────────────
     1  0.768448   0.768448
     2  0.940515   0.940515
     3  0.673959   0.673959
     4  0.395453   0.395453
     5  0.313244   0.313244
     6  0.662555   0.662555
     7  0.586022   0.586022
     8  0.0521332  0.0521332
     9  0.26864    0.26864
    10  0.108871   0.108871

julia> ts = TSFrame([random(10) random(10)], colnames=[:A, :B]) # columns are named A and B
(10 x 2) TSFrame with Int64 Index

 Index  A          B       
 Int64  Float64    Float64   
─────────────────────────────
     1  0.768448   0.768448
     2  0.940515   0.940515
     3  0.673959   0.673959
     4  0.395453   0.395453
     5  0.313244   0.313244
     6  0.662555   0.662555
     7  0.586022   0.586022
     8  0.0521332  0.0521332
     9  0.26864    0.26864
    10  0.108871   0.108871

julia> ts = TSFrame([random(10) random(10)], dates) 
(10 x 2) TSFrame with Date Index

 Index       x1         x2
 Date        Float64    Float64
──────────────────────────────────
 2017-01-01  0.768448   0.768448
 2017-01-02  0.940515   0.940515
 2017-01-03  0.673959   0.673959
 2017-01-04  0.395453   0.395453
 2017-01-05  0.313244   0.313244
 2017-01-06  0.662555   0.662555
 2017-01-07  0.586022   0.586022
 2017-01-08  0.0521332  0.0521332
 2017-01-09  0.26864    0.26864
 2017-01-10  0.108871   0.108871

julia> ts = TSFrame([random(10) random(10)], dates, colnames=[:A, :B]) # columns are named A and B
(10 x 2) TSFrame with Date Index

 Index       A          B         
 Date        Float64    Float64   
──────────────────────────────────
 2017-01-01  0.768448   0.768448
 2017-01-02  0.940515   0.940515
 2017-01-03  0.673959   0.673959
 2017-01-04  0.395453   0.395453
 2017-01-05  0.313244   0.313244
 2017-01-06  0.662555   0.662555
 2017-01-07  0.586022   0.586022
 2017-01-08  0.0521332  0.0521332
 2017-01-09  0.26864    0.26864
 2017-01-10  0.108871   0.108871

julia> ts = TSFrame(Int64; n=5) # empty TSFrame with 5 columns of type Any and with Int64 index type
0×5 TSFrame with Int64 Index

julia> ts = TSFrame(Date, [(Int64, :col1), (String, :col2), (Float64, :col3)]) # empty TSFrame with specific column names and types
0×3 TSFrame with Date Index

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.
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))
(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)

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);

julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10));

julia> ts1 = TSFrame(random(length(dates)), dates);
julia> show(ts1)
(10 x 1) TSFrame with Dates.Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871

julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,30));

julia> ts2 = TSFrame(random(length(dates)), dates);
julia> show(ts2)
30×1 TSFrame with Date Index
 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871
 2017-01-11  0.163666
 2017-01-12  0.473017
 2017-01-13  0.865412
 2017-01-14  0.617492
 2017-01-15  0.285698
 2017-01-16  0.463847
 2017-01-17  0.275819
 2017-01-18  0.446568
 2017-01-19  0.582318
 2017-01-20  0.255981
 2017-01-21  0.70586
 2017-01-22  0.291978
 2017-01-23  0.281066
 2017-01-24  0.792931
 2017-01-25  0.20923
 2017-01-26  0.918165
 2017-01-27  0.614255
 2017-01-28  0.802665
 2017-01-29  0.555668
 2017-01-30  0.940782

# join on all index values
# equivalent to `join(ts1, ts2; jointype=:JoinAll)` call
julia> join(ts1, ts2)
(30 x 2) TSFrame with Date Index
 Index       x1               x1_1
 Date        Float64?         Float64?
────────────────────────────────────────
 2017-01-01        0.768448   0.768448
 2017-01-02        0.940515   0.940515
 2017-01-03        0.673959   0.673959
 2017-01-04        0.395453   0.395453
 2017-01-05        0.313244   0.313244
 2017-01-06        0.662555   0.662555
 2017-01-07        0.586022   0.586022
 2017-01-08        0.0521332  0.0521332
 2017-01-09        0.26864    0.26864
 2017-01-10        0.108871   0.108871
     ⋮              ⋮             ⋮
 2017-01-22  missing          0.291978
 2017-01-23  missing          0.281066
 2017-01-24  missing          0.792931
 2017-01-25  missing          0.20923
 2017-01-26  missing          0.918165
 2017-01-27  missing          0.614255
 2017-01-28  missing          0.802665
 2017-01-29  missing          0.555668
 2017-01-30  missing          0.940782
                         11 rows omitted

# alias to `join()`
julia> cbind(ts1, ts2);

# join only the common index values
julia> join(ts1, ts2; jointype=:JoinBoth)
(10 x 2) TSFrame with Date Index
 Index       x1         x1_1
 Date        Float64    Float64
──────────────────────────────────
 2017-01-01  0.768448   0.768448
 2017-01-02  0.940515   0.940515
 2017-01-03  0.673959   0.673959
 2017-01-04  0.395453   0.395453
 2017-01-05  0.313244   0.313244
 2017-01-06  0.662555   0.662555
 2017-01-07  0.586022   0.586022
 2017-01-08  0.0521332  0.0521332
 2017-01-09  0.26864    0.26864
 2017-01-10  0.108871   0.108871

# keep index values of `ts1`
julia> join(ts1, ts2; jointype=:JoinLeft)
(10 x 2) TSFrame with Date Index
 Index       x1         x1_1
 Date        Float64    Float64?
──────────────────────────────────
 2017-01-01  0.768448   0.768448
 2017-01-02  0.940515   0.940515
 2017-01-03  0.673959   0.673959
 2017-01-04  0.395453   0.395453
 2017-01-05  0.313244   0.313244
 2017-01-06  0.662555   0.662555
 2017-01-07  0.586022   0.586022
 2017-01-08  0.0521332  0.0521332
 2017-01-09  0.26864    0.26864
 2017-01-10  0.108871   0.108871

# keep index values of `ts2`
julia> join(ts1, ts2; jointype=:JoinRight)
(30 x 2) TSFrame with Date Index
 Index       x1               x1_1
 Date        Float64?         Float64
────────────────────────────────────────
 2017-01-01        0.768448   0.768448
 2017-01-02        0.940515   0.940515
 2017-01-03        0.673959   0.673959
 2017-01-04        0.395453   0.395453
 2017-01-05        0.313244   0.313244
 2017-01-06        0.662555   0.662555
 2017-01-07        0.586022   0.586022
 2017-01-08        0.0521332  0.0521332
 2017-01-09        0.26864    0.26864
 2017-01-10        0.108871   0.108871
     ⋮              ⋮             ⋮
 2017-01-22  missing          0.291978
 2017-01-23  missing          0.281066
 2017-01-24  missing          0.792931
 2017-01-25  missing          0.20923
 2017-01-26  missing          0.918165
 2017-01-27  missing          0.614255
 2017-01-28  missing          0.802665
 2017-01-29  missing          0.555668
 2017-01-30  missing          0.940782
                         11 rows omitted

julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,30));

julia> ts3 = TSFrame(random(length(dates)), dates);
julia> show(ts3)
30×1 TSFrame with Date Index
 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871
 2017-01-11  0.163666
 2017-01-12  0.473017
 2017-01-13  0.865412
 2017-01-14  0.617492
 2017-01-15  0.285698
 2017-01-16  0.463847
 2017-01-17  0.275819
 2017-01-18  0.446568
 2017-01-19  0.582318
 2017-01-20  0.255981
 2017-01-21  0.70586
 2017-01-22  0.291978
 2017-01-23  0.281066
 2017-01-24  0.792931
 2017-01-25  0.20923
 2017-01-26  0.918165
 2017-01-27  0.614255
 2017-01-28  0.802665
 2017-01-29  0.555668
 2017-01-30  0.940782

# joining multiple TSFrame objects
julia> join(ts1, ts2, ts3; jointype=:JoinLeft)
10×3 TSFrame with Date Index
 Index       x1         x1_1       x1_2
 Date        Float64    Float64?   Float64?
─────────────────────────────────────────────
 2017-01-01  0.768448   0.768448   0.768448
 2017-01-02  0.940515   0.940515   0.940515
 2017-01-03  0.673959   0.673959   0.673959
 2017-01-04  0.395453   0.395453   0.395453
 2017-01-05  0.313244   0.313244   0.313244
 2017-01-06  0.662555   0.662555   0.662555
 2017-01-07  0.586022   0.586022   0.586022
 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;

julia> random(x) = rand(MersenneTwister(123), x);

julia> dates1 = collect(Date(2017,1,1):Day(1):Date(2017,1,10));

julia> dates2 = collect(Date(2017,1,11):Day(1):Date(2017,1,30));

julia> ts1 = TSFrame([randn(length(dates1)) randn(length(dates1))], dates1)
julia> show(ts1)
(10 x 1) TSFrame with Dates.Date Index

 Index       x1
 Date        Float64
────────────────────────
 2017-01-01  -0.420348
 2017-01-02   0.109363
 2017-01-03  -0.0702014
 2017-01-04   0.165618
 2017-01-05  -0.0556799
 2017-01-06  -0.147801
 2017-01-07  -2.50723
 2017-01-08  -0.099783
 2017-01-09   0.177526
 2017-01-10  -1.08461

julia> df = DataFrame(x1 = randn(length(dates2)), y1 = randn(length(dates2)))
julia> ts2 = TSFrame(df, dates2)
julia> show(ts2)
(20 x 1) TSFrame with Dates.Date Index

 Index       x1
 Date        Float64
────────────────────────
 2017-01-11   2.15087
 2017-01-12   0.9203
 2017-01-13  -0.0879142
 2017-01-14  -0.930109
 2017-01-15   0.061117
 2017-01-16   0.0434627
 2017-01-17   0.0834733
 2017-01-18  -1.52281
     ⋮           ⋮
 2017-01-23  -0.756143
 2017-01-24   0.491623
 2017-01-25   0.549672
 2017-01-26   0.570689
 2017-01-27  -0.380011
 2017-01-28  -2.09965
 2017-01-29   1.37289
 2017-01-30  -0.462384
          4 rows omitted


julia> vcat(ts1, ts2)
(30 x 3) TSFrame with Date Index

 Index       x1          x2              y1
 Date        Float64     Float64?        Float64?
─────────────────────────────────────────────────────────
 2017-01-01  -0.524798        -1.4949    missing
 2017-01-02  -0.719611        -1.1278    missing
 2017-01-03   0.0926092        1.19778   missing
 2017-01-04   0.236237         1.39115   missing
 2017-01-05   0.369588         1.21792   missing
 2017-01-06   1.65287         -0.930058  missing
 2017-01-07   0.761301         0.23794   missing
 2017-01-08  -0.571046        -0.480486  missing
 2017-01-09  -2.01905         -0.46391   missing
 2017-01-10   0.193942        -1.01471   missing
 2017-01-11   0.239041   missing              -0.473429
 2017-01-12   0.286036   missing              -0.90377
 2017-01-13   0.683429   missing              -0.128489
 2017-01-14  -1.51442    missing              -2.39843
 2017-01-15  -0.581341   missing              -0.12265
 2017-01-16   1.07059    missing              -0.916064
 2017-01-17   0.859396   missing               0.0162969
 2017-01-18  -1.93127    missing               2.11127
 2017-01-19   0.529477   missing               0.636964
 2017-01-20   0.817429   missing              -0.34038
 2017-01-21  -0.682296   missing              -0.971262
 2017-01-22   1.36232    missing              -0.236323
 2017-01-23   0.143188   missing              -0.501722
 2017-01-24   0.621845   missing              -1.20016
 2017-01-25   0.076199   missing              -1.36616
 2017-01-26   0.379672   missing              -0.555395
 2017-01-27   0.494473   missing               1.05389
 2017-01-28   0.278259   missing              -0.358983
 2017-01-29   0.0231765  missing               0.712526
 2017-01-30   0.516704   missing               0.216855

julia> vcat(ts1, ts2; colmerge=:intersect)
(30 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
────────────────────────
 2017-01-01  -0.524798
 2017-01-02  -0.719611
 2017-01-03   0.0926092
 2017-01-04   0.236237
 2017-01-05   0.369588
 2017-01-06   1.65287
 2017-01-07   0.761301
 2017-01-08  -0.571046
 2017-01-09  -2.01905
 2017-01-10   0.193942
 2017-01-11   0.239041
 2017-01-12   0.286036
 2017-01-13   0.683429
 2017-01-14  -1.51442
 2017-01-15  -0.581341
 2017-01-16   1.07059
 2017-01-17   0.859396
 2017-01-18  -1.93127
 2017-01-19   0.529477
 2017-01-20   0.817429
 2017-01-21  -0.682296
 2017-01-22   1.36232
 2017-01-23   0.143188
 2017-01-24   0.621845
 2017-01-25   0.076199
 2017-01-26   0.379672
 2017-01-27   0.494473
 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)))
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);
julia> dates = Date("2022-01-01"):Month(1):Date("2022-01-01")+Month(11);

julia> df = DataFrame(Index = dates,
        val1 = random(12),
        val2 = random(12),
        val3 = random(12));

julia> ts = TSFrame(df)
julia> show(ts)
(12 x 3) TSFrame with Dates.Date Index

 Index       val1        val2        val3
 Date        Float64     Float64     Float64
────────────────────────────────────────────────
 2022-01-01  -0.319954    0.974594   -0.552977
 2022-02-01  -0.0386735  -0.171675    0.779539
 2022-03-01   1.67678    -1.75251     0.820462
 2022-04-01   1.69702    -0.0130037   1.0507
 2022-05-01   0.992128    0.76957    -1.28008
 2022-06-01  -0.315461   -0.543976   -0.117256
 2022-07-01  -1.18952    -1.12867    -0.0829082
 2022-08-01   0.159595    0.450044   -0.231828
 2022-09-01   0.501436    0.265327   -0.948532
 2022-10-01  -2.10516    -1.11489     0.285194
 2022-11-01  -0.781082   -1.20202    -0.639953
 2022-12-01  -0.169184    1.34879     1.33361


julia> using Plots

julia> # plot(ts)

# plot first 6 rows with selected columns
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.applyMethod

Apply/Period conversion

apply(ts::TSFrame,
      period::T,
      fun::V,
      index_at::Function=first;
      renamecols::Bool=true)
     where {T<:Dates.Period, V <: Function}

Apply fun to ts object based on period and return correctly indexed rows. This method is used for doing aggregation over a time period or to convert ts into an object of lower frequency (ex. from daily series to monthly).

period is any of Period types in the Dates module. Conversion from lower to a higher frequency will throw an error as interpolation isn't currently handled by this method.

By default, the method uses the first value of the index within the period to index the resulting aggregated object. This behaviour can be controlled by index_at argument which can take first or last as an input.

Keyword arguments

  • renamecols::Bool=true: whether to rename column names in the resulting object. If false, the column name is automatically generated based on the name of fun otherwise existing column names are used.

Examples

julia> using Random, Statistics;
julia> random(x) = rand(MersenneTwister(123), x);
julia> dates = collect(Date(2017,1,1):Day(1):Date(2018,3,10));

julia> ts = TSFrame(random(length(dates)), dates)
julia> show(ts[1:10])
(10 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871

julia> apply(ts, Month(1), first)
(15 x 1) TSFrame with Date Index

 Index       x1_first
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-02-01  0.790201
 2017-03-01  0.467219
 2017-04-01  0.783473
 2017-05-01  0.651354
 2017-06-01  0.373346
 2017-07-01  0.83296
 2017-08-01  0.132716
 2017-09-01  0.27899
 2017-10-01  0.995414
 2017-11-01  0.214132
 2017-12-01  0.832917
 2018-01-01  0.0409471
 2018-02-01  0.720163
 2018-03-01  0.87459

# alternate months
julia> apply(ts, Month(2), first)
(8 x 1) TSFrame with Date Index

 Index       x1_first
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-03-01  0.467219
 2017-05-01  0.651354
 2017-07-01  0.83296
 2017-09-01  0.27899
 2017-11-01  0.214132
 2018-01-01  0.0409471
 2018-03-01  0.87459


julia> ts_weekly = apply(ts, Week(1), Statistics.std) # weekly standard deviation
julia> show(ts_weekly[1:10])
(10 x 1) TSFrame with Date Index

 Index       x1_std
 Date        Float64
────────────────────────
 2017-01-01  NaN
 2017-01-02    0.28935
 2017-01-09    0.270842
 2017-01-16    0.170197
 2017-01-23    0.269573
 2017-01-30    0.326687
 2017-02-06    0.279935
 2017-02-13    0.319216
 2017-02-20    0.272058
 2017-02-27    0.23651


julia> ts_weekly = apply(ts, Week(1), Statistics.std, last) # indexed by last date of the week
julia> show(ts_weekly[1:10])
(10 x 1) TSFrame with Date Index

 Index       x1_std
 Date        Float64
────────────────────────
 2017-01-01  NaN
 2017-01-08    0.28935
 2017-01-15    0.270842
 2017-01-22    0.170197
 2017-01-29    0.269573
 2017-02-05    0.326687
 2017-02-12    0.279935
 2017-02-19    0.319216
 2017-02-26    0.272058
 2017-03-05    0.23651
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)))
julia> describe(ts)
2×7 DataFrame
 Row │ variable  mean     min    median   max    nmissing  eltype
     │ Symbol    Float64  Int64  Float64  Int64  Int64     Type
─────┼───────────────────────────────────────────────────────────────────────────
   1 │ Index        5.5       1      5.5     10         0  Int64
   2 │ x1           2.75      2      3.0      4         2  Union{Missing, Int64}
julia> describe(ts, cols=:Index)
1×7 DataFrame
 Row │ variable  mean     min    median   max    nmissing  eltype
     │ Symbol    Float64  Int64  Float64  Int64  Int64     DataType
─────┼──────────────────────────────────────────────────────────────
   1 │ Index         5.5      1      5.5     10         0  Int64
julia> describe(ts, :min, :max, cols=:x1)
1×3 DataFrame
 Row │ variable  min    max
     │ Symbol    Int64  Int64
─────┼────────────────────────
   1 │ x1            2      4
julia> describe(ts, :min, sum => :sum)
2×3 DataFrame
 Row │ variable  min    sum
     │ Symbol    Int64  Int64
─────┼────────────────────────
   1 │ Index         1     55
   2 │ x1            2     22
julia> describe(ts, :min, sum => :sum, cols=:x1)
1×3 DataFrame
 Row │ variable  min    sum
     │ 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}, V<:Dates.Period}
endpoints(ts::TSFrame, on::T) where {T<:Dates.Period}
endpoints(ts::TSFrame, on::Symbol, k::Int=1)
endpoints(ts::TSFrame, on::String, k::Int=1)
endpoints(ts::TSFrame, on::Function, k::Int=1)
endpoints(values::AbstractVector, on::Function, k::Int=1)

Return an integer vector of values for last observation in timestamps for each period given by on. The values are picked up every on.value instance of the period.

Can be used to subset a TSFrame object directly using this function's return value. The methods work for regular time series of any periodicity and irregular time series belonging to any of the time-based types provided by the Dates module.

The primary method works for series of all time types including Date, DateTime, and Time, and for on belonging to any of the sub-types of Dates.Period. The ::TSFrame methods are provided for convenience and call the primary method directly using the Index column.

For the methods accepting on of Function type the values vector will get converted into unique period-groups which act as unique keys. The method uses these keys to create groups of values and uses the period provided by on to pick up the last observation in each group. k decides the number of groups to skip. For example, k=2 picks every alternate group starting from the 2ⁿᵈ element out of the ones created by on. See the examples below to see how the function works in the real world. The on function should return a Vector to be used as grouping keys.

endpoints(ts::TSFrame, on::Symbol) and endpoints(ts::TSFrame, on::String) are convenience methods where valid values for on are: :years, :quarters, :months, :weeks, :days, :hours, :minutes, :seconds, :milliseconds, :microseconds, and :nanoseconds.

Note, that except for on::Function all other methods expect Index type of TSFrame to be a subtype of TimeType.

The method returns Vector{Int} corresponding to the matched values in the first argument.

Examples

julia> using Random
julia> random(x) = rand(MersenneTwister(123), x);
julia> dates = Date(2017):Day(1):Date(2019);
julia> ts = TSFrame(random(length(dates)), dates)
(731 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871
     ⋮           ⋮
 2018-12-24  0.812797
 2018-12-25  0.158056
 2018-12-26  0.269285
 2018-12-27  0.15065
 2018-12-28  0.916177
 2018-12-29  0.278016
 2018-12-30  0.617211
 2018-12-31  0.67549
 2019-01-01  0.910285
       712 rows omitted

julia> ep = endpoints(ts, Month(1))
25-element Vector{Int64}:
  31
  59
  90
 120
 151
 181
 212
 243
 273
 304
 334
 365
 396
 424
 455
 485
 516
 546
 577
 608
 638
 669
 699
 730
 731

julia> ts[ep]
(25 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-31  0.48
 2017-02-28  0.458476
 2017-03-31  0.274441
 2017-04-30  0.413966
 2017-05-31  0.734931
 2017-06-30  0.257159
 2017-07-31  0.415851
 2017-08-31  0.0377973
 2017-09-30  0.934059
 2017-10-31  0.413175
 2017-11-30  0.557009
 2017-12-31  0.346659
 2018-01-31  0.174777
 2018-02-28  0.432223
 2018-03-31  0.835142
 2018-04-30  0.945539
 2018-05-31  0.0635483
 2018-06-30  0.589922
 2018-07-31  0.285088
 2018-08-31  0.912558
 2018-09-30  0.238931
 2018-10-31  0.49775
 2018-11-30  0.830232
 2018-12-31  0.67549
 2019-01-01  0.910285

julia> diff(index(ts[ep]))
24-element Vector{Day}:
 28 days
 31 days
 30 days
 31 days
 30 days
 31 days
 31 days
 30 days
 31 days
 30 days
 31 days
 31 days
 28 days
 31 days
 30 days
 31 days
 30 days
 31 days
 31 days
 30 days
 31 days
 30 days
 31 days
 1 day

# every 2ⁿᵈ month
julia> ts[endpoints(ts, Month(2))]
(12 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-02-28  0.458476
 2017-04-30  0.413966
 2017-06-30  0.257159
 2017-08-31  0.0377973
 2017-10-31  0.413175
 2017-12-31  0.346659
 2018-02-28  0.432223
 2018-04-30  0.945539
 2018-06-30  0.589922
 2018-08-31  0.912558
 2018-10-31  0.49775
 2018-12-31  0.67549
 2019-01-01  0.910285

# Weekly points using a function
julia> endpoints(ts, i -> lastdayofweek.(i), 1)
106-element Vector{Int64}:
   1
   8
  15
  22
  29
  36
  43
   ⋮
 694
 701
 708
 715
 722
 729
 731

julia> endpoints(ts, i -> lastdayofweek.(i), 1) == endpoints(ts, Week(1))
true

# Time type series
julia> timestampsminutes = collect(Time(9, 1, 2):Minute(1):Time(11, 2, 3));
julia> timestampsminutes[endpoints(timestampsminutes, Minute(2))]
61-element Vector{Time}:
 09:02:02
 09:04:02
 09:06:02
 09:08:02
 09:10:02
 09:12:02
 09:14:02
 09:16:02
 09:18:02
 09:20:02
 09:22:02
 09:24:02
 ⋮
 10:40:02
 10:42:02
 10:44:02
 10:46:02
 10:48:02
 10:50:02
 10:52:02
 10:54:02
 10:56:02
 10:58:02
 11:00:02
 11:02:02

julia> timestampsminutes[endpoints(timestampsminutes, Hour(1))]
3-element Vector{Time}:
 09:59:02
 10:59:02
 11:02:02

## Irregular series
julia> datetimeseconds = collect(range(DateTime(2022, 10, 08) + Hour(9),
                                DateTime(2022, 10, 08) + Hour(15) + Minute(29),
                                step=Second(1)));
julia> datetimesecondsrandom = sample(MersenneTwister(123), datetimeseconds, 20, replace=false, ordered=true)
17-element Vector{DateTime}:
 2022-10-08T09:20:16
 2022-10-08T09:32:00
 2022-10-08T09:43:57
 2022-10-08T10:13:27
 2022-10-08T10:44:34
 2022-10-08T11:04:23
 2022-10-08T11:08:37
 2022-10-08T11:46:51
 2022-10-08T11:56:46
 2022-10-08T12:14:22
 2022-10-08T12:32:08
 2022-10-08T13:28:42
 2022-10-08T13:34:33
 2022-10-08T13:54:11
 2022-10-08T13:59:08
 2022-10-08T14:05:57
 2022-10-08T14:37:17

julia> datetimesecondsrandom[endpoints(datetimesecondsrandom, Hour(1))]
6-element Vector{DateTime}:
 2022-10-08T09:43:57
 2022-10-08T10:44:34
 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))
(10 x 1) TSFrame 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
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);

julia> ts = TSFrame(random(10), Date("2022-02-01"):Month(1):Date("2022-02-01")+Month(9));


julia> show(ts)
(10 x 1) TSFrame with Dates.Date Index

 Index       x1
 Date        Float64
───────────────────────
 2022-02-01  0.768448
 2022-03-01  0.940515
 2022-04-01  0.673959
 2022-05-01  0.395453
 2022-06-01  0.313244
 2022-07-01  0.662555
 2022-08-01  0.586022
 2022-09-01  0.0521332
 2022-10-01  0.26864
 2022-11-01  0.108871

julia> index(ts)
10-element Vector{Date}:
 2022-02-01
 2022-03-01
 2022-04-01
 2022-05-01
 2022-06-01
 2022-07-01
 2022-08-01
 2022-09-01
 2022-10-01
 2022-11-01

julia>  eltype(index(ts))
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)
isregular(ts::TSFrame, unit::T) where {T<:Dates.Period}

Check if ts is regular.

  • if unit == :firstdiff then it checks if first difference of index is constant

  • otherwise it will check if first difference is constant with respect to specified timeperiod.

For eg. if unit == :month then check if first difference is constant with respect to months.

Examples

julia> using Random;
julia> random(x) = rand(MersenneTwister(123), x);

julia> dates=collect(Date(2017,1,1):Day(1):Date(2017,1,10))
10-element Vector{Date}:
 2017-01-01
 2017-01-02
 2017-01-03
 2017-01-04
 2017-01-05
 2017-01-06
 2017-01-07
 2017-01-08
 2017-01-09
 2017-01-10

julia> isregular(dates) # check if regular
true

julia> isregular(dates, Day(1)) # check if regular with a time difference of 1 day
true

julia> isregular(dates, Day(2)) # check if regular with a time difference of 2 days
false

julia> isregular(dates, :month) # check if regular with respect to months
false

julia> ts = TSFrame(random(10), dates)
10×1 TSFrame with Date Index
 Index       x1        
 Date        Float64   
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871

julia> isregular(ts)
true

julia> isregular(ts, Day(1))
true

julia> isregular(ts, Day(2))
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;

julia> random(x) = rand(MersenneTwister(123), x);

julia> dates = collect(Date(2017,1,1):Day(1):Date(2017,1,10));

julia> ts = TSFrame(random(length(dates)), dates);
julia> show(ts)
(10 x 1) TSFrame with Dates.Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
 2017-01-09  0.26864
 2017-01-10  0.108871


julia> lag(ts)
(10 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64?
─────────────────────────────
 2017-01-01  missing
 2017-01-02        0.768448
 2017-01-03        0.940515
 2017-01-04        0.673959
 2017-01-05        0.395453
 2017-01-06        0.313244
 2017-01-07        0.662555
 2017-01-08        0.586022
 2017-01-09        0.0521332
 2017-01-10        0.26864

julia> lag(ts, 2) # lags by 2 values
(10 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64?
─────────────────────────────
 2017-01-01  missing
 2017-01-02  missing
 2017-01-03        0.768448
 2017-01-04        0.940515
 2017-01-05        0.673959
 2017-01-06        0.395453
 2017-01-07        0.313244
 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;

julia> random(x) = rand(MersenneTwister(123), x);

julia> dates = collect(Date(2017,1,1):Day(1):Date(2018,3,10));

julia> ts = TSFrame(DataFrame(Index = dates, x1 = random(length(dates))))
julia> show(ts)
(434 x 1) TSFrame with Dates.Date Index

 Index       x1
 Date        Float64
───────────────────────
 2017-01-01  0.768448
 2017-01-02  0.940515
 2017-01-03  0.673959
 2017-01-04  0.395453
 2017-01-05  0.313244
 2017-01-06  0.662555
 2017-01-07  0.586022
 2017-01-08  0.0521332
     ⋮           ⋮
 2018-03-03  0.127635
 2018-03-04  0.147813
 2018-03-05  0.873555
 2018-03-06  0.486486
 2018-03-07  0.495525
 2018-03-08  0.64075
 2018-03-09  0.375126
 2018-03-10  0.0338698
       418 rows omitted


julia> lead(ts)[1:10]        # leads once
(10 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64?
───────────────────────
 2017-01-01  0.940515
 2017-01-02  0.673959
 2017-01-03  0.395453
 2017-01-04  0.313244
 2017-01-05  0.662555
 2017-01-06  0.586022
 2017-01-07  0.0521332
 2017-01-08  0.26864
 2017-01-09  0.108871
 2017-01-10  0.163666

julia> lead(ts, 2)[1:10]     # leads by 2 values
(10 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64?
───────────────────────
 2017-01-01  0.673959
 2017-01-02  0.395453
 2017-01-03  0.313244
 2017-01-04  0.662555
 2017-01-05  0.586022
 2017-01-06  0.0521332
 2017-01-07  0.26864
 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;

julia> random(x) = rand(MersenneTwister(123), x);

julia> TSFrames.ncol(TSFrame([random(100) random(100) random(100)]))
3

julia> nc(TSFrame([random(100) random(100) random(100)]))
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)
rename!(ts::TSFrame, colnames::AbstractVector{Symbol}; makeunique::Bool=false)
rename!(ts::TSFrame, (from => to)::Pair...)
rename!(ts::TSFrame, d::AbstractDict)
rename!(ts::TSFrame, d::AbstractVector{<:Pair})
rename!(f::Function, ts::TSFrame)

Renames columns of ts in-place. The interface is similar to DataFrames.jl's renaming interface.

Arguments

  • ts: the TSFrame.
  • colnames: an AbstractVector containing Strings or Symbols. Must be of the same length as the number of non-Index columns in ts, and cannot contain the string "Index" or the symbol :Index.
  • makeunique: if false (which is the default), an error will be raised if colnames contains duplicate names. If true, then duplicate names will be suffixed with _i (i starting at 1 for the first duplicate).
  • d: an AbstractDict or an AbstractVector of Pairs that maps original names to new names. Cannot map :Index to any other column name.
  • f: a function which for each non-Index column takes the old name as a String and returns the new name as a String.

If pairs are passed to rename! (as positional arguments or as a dictionary or as a vector), then

  • from can be a String or a Symbol.
  • to can be a String or a Symbol.
  • Mixing Strings and Symbols in from and to is not allowed.
julia> ts = TSFrame(DataFrame(Index=Date(2012, 1, 1):Day(1):Date(2012, 1, 10), x1=1:10, x2=11:20))
10×2 TSFrame with Date Index
 Index       x1     x2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 2012-01-08      8     18
 2012-01-09      9     19
 2012-01-10     10     20

julia> TSFrames.rename!(ts, ["X1", "X2"])
 10×2 TSFrame with Date Index
 Index       X1     X2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 2012-01-08      8     18
 2012-01-09      9     19
 2012-01-10     10     20

julia> TSFrames.rename!(ts, [:x1, :x2])
10×2 TSFrame with Date Index
 Index       x1     x2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 2012-01-08      8     18
 2012-01-09      9     19
 2012-01-10     10     20

julia> TSFrames.rename!(ts, :x1 => :X1, :x2 => :X2)
10×2 TSFrame with Date Index
 Index       X1     X2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 2012-01-08      8     18
 2012-01-09      9     19
 2012-01-10     10     20

julia> TSFrames.rename!(ts, Dict("X1" => :x1, "X2" => :x2))
 10×2 TSFrame with Date Index
 Index       x1     x2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 2012-01-08      8     18
 2012-01-09      9     19
 2012-01-10     10     20

julia> TSFrames.rename!(ts, [:x1 => "X1", :x2 => "X2"])
10×2 TSFrame with Date Index
 Index       X1     X2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 2012-01-08      8     18
 2012-01-09      9     19
 2012-01-10     10     20

julia> TSFrames.rename(lowercase, ts)
10×2 TSFrame with Date Index
 Index       x1     x2
 Date        Int64  Int64
──────────────────────────
 2012-01-01      1     11
 2012-01-02      2     12
 2012-01-03      3     13
 2012-01-04      4     14
 2012-01-05      5     15
 2012-01-06      6     16
 2012-01-07      7     17
 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)
6×2 TSFrame with Int64 Index
 Index  rolling_x1_mean  rolling_x2_mean 
 Int64  Float64          Float64         
─────────────────────────────────────────
     5              3.0             13.0
     6              4.0             14.0
     7              5.0             15.0
     8              6.0             16.0
     9              7.0             17.0
    10              8.0             18.0

julia> dates = Date(2001, 1, 1):Day(1):Date(2001, 1, 10);
julia> df = DataFrame(Index=dates, inrchf=1:10, usdchf=1:10, eurchf=1:10, gbpchf=1:10, jpychf=1:10);
julia> ts = TSFrame(df)
10×5 TSFrame with Date Index
 Index       inrchf  usdchf  eurchf  gbpchf  jpychf 
 Date        Int64   Int64   Int64   Int64   Int64  
────────────────────────────────────────────────────
 2001-01-01       1       1       1       1       1
 2001-01-02       2       2       2       2       2
 2001-01-03       3       3       3       3       3
 2001-01-04       4       4       4       4       4
 2001-01-05       5       5       5       5       5
 2001-01-06       6       6       6       6       6
 2001-01-07       7       7       7       7       7
 2001-01-08       8       8       8       8       8
 2001-01-09       9       9       9       9       9
 2001-01-10      10      10      10      10      10

julia> function regress(ts)     # defining function for multiple regressions
            ll = lm(@formula(inrchf ~ usdchf + eurchf + gbpchf + jpychf), ts.coredata[:, Not(:Index)])
            co = coef(ll)[coefnames(ll) .== "usdchf"]
            sd = Statistics.std(residuals(ll))
            return (co, sd)
       end

julia> rollapply(ts, regress, 5; bycolumn=false)    # doing multiple regressions
6×1 TSFrame with Date Index
 Index       rolling_regress      
 Date        Tuple…               
──────────────────────────────────
 2001-01-05  ([1.0], 9.93014e-17)
 2001-01-06  ([1.0], 1.27168e-15)
 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;
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)
julia> show(ts)
(40 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2022-02-01  0.768448
 2022-02-08  0.940515
 2022-02-15  0.673959
 2022-02-22  0.395453
 2022-03-01  0.313244
 2022-03-08  0.662555
 2022-03-15  0.586022
 2022-03-22  0.0521332
 2022-03-29  0.26864
 2022-04-05  0.108871
 2022-04-12  0.163666
 2022-04-19  0.473017
 2022-04-26  0.865412
 2022-05-03  0.617492
 2022-05-10  0.285698
 2022-05-17  0.463847
 2022-05-24  0.275819
 2022-05-31  0.446568
 2022-06-07  0.582318
 2022-06-14  0.255981
 2022-06-21  0.70586
 2022-06-28  0.291978
 2022-07-05  0.281066
 2022-07-12  0.792931
 2022-07-19  0.20923
 2022-07-26  0.918165
 2022-08-02  0.614255
 2022-08-09  0.802665
 2022-08-16  0.555668
 2022-08-23  0.940782
 2022-08-30  0.48
 2022-09-06  0.790201
 2022-09-13  0.356221
 2022-09-20  0.900925
 2022-09-27  0.529253
 2022-10-04  0.031831
 2022-10-11  0.900681
 2022-10-18  0.940299
 2022-10-25  0.621379
 2022-11-01  0.348173

julia> subset(ts, Date(2022, 03), Date(2022, 07))
(18 x 1) TSFrame with Date Index

 Index       x1
 Date        Float64
───────────────────────
 2022-03-01  0.313244
 2022-03-08  0.662555
 2022-03-15  0.586022
 2022-03-22  0.0521332
 2022-03-29  0.26864
 2022-04-05  0.108871
 2022-04-12  0.163666
 2022-04-19  0.473017
 2022-04-26  0.865412
 2022-05-03  0.617492
 2022-05-10  0.285698
 2022-05-17  0.463847
 2022-05-24  0.275819
 2022-05-31  0.446568
 2022-06-07  0.582318
 2022-06-14  0.255981
 2022-06-21  0.70586
 2022-06-28  0.291978

julia> subset(TSFrame(1:20, -9:10), -4, 5)
(10 x 1) TSFrame with Int64 Index

 Index  x1
 Int64  Int64
──────────────
    -4      6
    -3      7
    -2      8
    -1      9
     0     10
     1     11
     2     12
     3     13
     4     14
     5     15

julia> subset(ts,:,Date("2022-04-12"))
(11 x 1) TSFrame with Date Index

 Index       x1        
 Date        Float64   
───────────────────────
 2022-02-01  0.768448
 2022-02-08  0.940515
 2022-02-15  0.673959
 2022-02-22  0.395453
 2022-03-01  0.313244
 2022-03-08  0.662555
 2022-03-15  0.586022
 2022-03-22  0.0521332
 2022-03-29  0.26864
 2022-04-05  0.108871
 2022-04-12  0.163666

julia> subset(ts,Date("2022-9-27"),:)
(6 x 1) TSFrame with Date Index

 Index       x1       
 Date        Float64  
──────────────────────
 2022-09-27  0.529253
 2022-10-04  0.031831
 2022-10-11  0.900681
 2022-10-18  0.940299
 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))
(10 x 1) TSFrame with Int64 Index

 Index  x1
 Int64  Int64
──────────────
    91     91
    92     92
    93     93
    94     94
    95     95
    96     96
    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}
to_yearly(tsf::TSFrame, n=1)::TSFrame
to_quarterly(tsf::TSFrame, n=1)::TSFrame
to_monthly(tsf::TSFrame, n=1)::TSFrame
to_weekly(tsf::TSFrame, n=1)::TSFrame
to_daily(tsf::TSFrame, n=1)::TSFrame
to_hourly(tsf::TSFrame, n=1)::TSFrame
to_minutes(tsf::TSFrame, n=1)::TSFrame
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