TimeSeries.jl

repository·master·Indexed 18 days ago

https://github.com/juliastats/timeseries.jl

A lightweight framework for working with time series data in Julia. It provides the `TimeArray` abstraction for handling time-indexed data using Date and DateTime objects. The library includes tools for data manipulation such as lagging, leading, finite differences (`diff`), percent change, sliding window operations (`moving`), and cumulative aggregations (`upto`, `basecall`). It also supports joining (`merge`), compressing (`collapse`), and concatenating (`vcat`, `hcat`) time series, as well as flexible indexing by integers, dates, and symbols.

Tokens
8.5K
Snippets
49
Records
54
Agent score
63%

What's inside TimeSeries.jl

  1. Understand the `TimeArray` data structure

    master

    A TimeArray is the core time series type in TimeSeries.jl. It represents a collection of data points associated with specific timestamps. It is composed of four primary fields:

    1. timestamp: A sorted Vector of TimeType (typically Date or DateTime). Timestamps must be sequential; non-sequential dates will cause construction to fail.
    2. values: An AbstractArray containing the actual data. The number of rows in values must exactly match the length of the timestamp vector. All elements in values must share the same type.
    3. colnames: A Vector{Symbol} providing names for each column in values. The length must match the column count of values. If duplicate names are provided, the constructor automatically appends _n (e.g., name, name_1, name_2) to ensure uniqueness for indexing.
    4. meta: A field for arbitrary metadata (defaults to nothing). It can hold Strings or custom user-defined types to store object-level information.
    struct TimeArray{T,N,D<:TimeType,A<:AbstractArray{T,N}} <: AbstractTimeSeries{T,N,D}
        timestamp::Vector{D}
        values::A
        colnames::Vector{Symbol}
        meta::Any
    end
  2. Use Base method extensions with TimeArray

    master

    TimeSeries.jl extends several standard Julia Base methods to provide specialized behavior for TimeArray objects. This allows you to perform common operations like concatenation, mapping, and arithmetic directly on time-series data using familiar Julia syntax.

    # Examples of supported Base extensions:
    # Concatenation (Horizontal and Vertical)
    hcat(time_array1, time_array2)
    vcat(time_array1, time_array2, ...)
    
    # Arithmetic
    result = time_array1 + time_array2
    diff = time_array1 - time_array2
    
    # Functional operations
    mapped_array = map(f, time_array)
    split_array = split(time_array, f)
    
    # Comparison and Iteration
    equality = (time_array1 == time_array2)
    for row in eachrow(time_array)
        # ...
    end
  3. Plot TimeSeries data using Plots.jl

    master

    The TimeSeries package provides a plotting recipe compatible with the Plots.jl framework. Note that TimeSeries does not automatically install any plotting packages; you must ensure Plots.jl (and a backend like gr()) is installed and loaded in your environment.

    using Plots, TimeSeries
    # Assuming 'ta' is a TimeArray object
    plot(ta)
  4. Iterate over rows and columns using Tables.jl interface

    master

    Because TimeSeries.jl integrates with the Tables.jl interface, you can use standard Julia iteration functions like eachrow and eachcol to traverse tabular data. When iterating over a TimeArray, the time index is treated as a regular data column named timestamp.

    using MarketData
    for row in eachrow(ohlc)
        time = row.timestamp
        c = row.Close
        # ...
    end
  5. Convert a `DataFrame` to a `TimeArray`

    master

    To convert a DataFrame back into a TimeArray, you must specify which column should serve as the time index using the timestamp keyword argument.

    # Example where the timestamp column is named :A
    df′ = DataFrames.rename(df, :timestamp => :A);
    first(df′)
    TimeArray(df′; timestamp=:A)
  6. Create dummy time series data

    master

    If you do not want to use MarketData, you can create dummy data manually using Dates and TimeSeries.TimeArray. This involves generating a range of dates and pairing them with a vector of random values.

    using Dates
    using TimeSeries
    
    dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31)
    ta = TimeArray(dates, rand(length(dates)))
  7. Create a TimeArray with Date objects

    master

    You can create a TimeArray using Date objects to represent daily frequency. This is useful for data indexed by calendar days.

    using TimeSeries
    using Dates
    
    dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31)
    ta = TimeArray(dates, rand(length(dates)))
  8. Plot multiple series from a TimeArray

    master

    You can pass a TimeArray containing multiple variables to the plot function. The recipe will plot each variable as an individual line, automatically aligning all variables to the same y-axis.

    # Plotting specific columns from a TimeArray
    plot(ta[:Open, :High, :Low, :Close])
    savefig("multi-series.svg")