Modify existing TimeArrays

Modify existing TimeArrays

Since TimeArrays are immutable, they cannot be altered or changed in-place. In practical application, an existing TimeArray might need to be used to create a new one with many of the same values. This might be thought of as changing the fields of an existing TimeArray, but what actually happens is a new TimeArray is created. To allow the use of an existing TimeArray to create a new one, the update and rename methods are provided.

update

The update method supports adding new observations only. Older and in-between dates are not supported:

julia> using TimeSeries

julia> using MarketData

julia> update(cl, Date(2002,1,1), 111.11)
501×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2002-01-01
│            │ Close  │
├────────────┼────────┤
│ 2000-01-03 │ 111.94 │
│ 2000-01-04 │ 102.5  │
│ 2000-01-05 │ 104.0  │
│ 2000-01-06 │ 95.0   │
│ 2000-01-07 │ 99.5   │
│ 2000-01-10 │ 97.75  │
│ 2000-01-11 │ 92.75  │
│ 2000-01-12 │ 87.19  │
│ 2000-01-13 │ 96.75  │
   ⋮
│ 2001-12-20 │ 20.67  │
│ 2001-12-21 │ 21.0   │
│ 2001-12-24 │ 21.36  │
│ 2001-12-26 │ 21.49  │
│ 2001-12-27 │ 22.07  │
│ 2001-12-28 │ 22.43  │
│ 2001-12-31 │ 21.9   │
│ 2002-01-01 │ 111.11 │

julia> update(cl, Date(2002,1,1), [111.11])
501×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2002-01-01
│            │ Close  │
├────────────┼────────┤
│ 2000-01-03 │ 111.94 │
│ 2000-01-04 │ 102.5  │
│ 2000-01-05 │ 104.0  │
│ 2000-01-06 │ 95.0   │
│ 2000-01-07 │ 99.5   │
│ 2000-01-10 │ 97.75  │
│ 2000-01-11 │ 92.75  │
│ 2000-01-12 │ 87.19  │
│ 2000-01-13 │ 96.75  │
   ⋮
│ 2001-12-20 │ 20.67  │
│ 2001-12-21 │ 21.0   │
│ 2001-12-24 │ 21.36  │
│ 2001-12-26 │ 21.49  │
│ 2001-12-27 │ 22.07  │
│ 2001-12-28 │ 22.43  │
│ 2001-12-31 │ 21.9   │
│ 2002-01-01 │ 111.11 │

julia> update(ohlc, Date(2002,1,1), [111.11 222.22 333.33 444.44])
501×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2002-01-01
│            │ Open   │ High   │ Low    │ Close  │
├────────────┼────────┼────────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5  │ 101.69 │ 111.94 │
│ 2000-01-04 │ 108.25 │ 110.62 │ 101.19 │ 102.5  │
│ 2000-01-05 │ 103.75 │ 110.56 │ 103.0  │ 104.0  │
│ 2000-01-06 │ 106.12 │ 107.0  │ 95.0   │ 95.0   │
│ 2000-01-07 │ 96.5   │ 101.0  │ 95.5   │ 99.5   │
│ 2000-01-10 │ 102.0  │ 102.25 │ 94.75  │ 97.75  │
│ 2000-01-11 │ 95.94  │ 99.38  │ 90.5   │ 92.75  │
│ 2000-01-12 │ 95.0   │ 95.5   │ 86.5   │ 87.19  │
│ 2000-01-13 │ 94.48  │ 98.75  │ 92.5   │ 96.75  │
   ⋮
│ 2001-12-20 │ 21.4   │ 21.47  │ 20.62  │ 20.67  │
│ 2001-12-21 │ 21.01  │ 21.54  │ 20.8   │ 21.0   │
│ 2001-12-24 │ 20.9   │ 21.45  │ 20.9   │ 21.36  │
│ 2001-12-26 │ 21.35  │ 22.3   │ 21.14  │ 21.49  │
│ 2001-12-27 │ 21.58  │ 22.25  │ 21.58  │ 22.07  │
│ 2001-12-28 │ 21.97  │ 23.0   │ 21.96  │ 22.43  │
│ 2001-12-31 │ 22.51  │ 22.66  │ 21.83  │ 21.9   │
│ 2002-01-01 │ 111.11 │ 222.22 │ 333.33 │ 444.44 │

rename and rename!

The rename method allows the column name(s) to be changed. The rename! is used for in-place update.

julia> using TimeSeries

julia> using MarketData

julia> rename(cl, :Close′) |> first
1×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2000-01-03
│            │ Close′ │
├────────────┼────────┤
│ 2000-01-03 │ 111.94 │

julia> rename(cl, [:Close′]) |> first
1×1 TimeArray{Float64,1,Date,Array{Float64,1}} 2000-01-03 to 2000-01-03
│            │ Close′ │
├────────────┼────────┤
│ 2000-01-03 │ 111.94 │

julia> rename(ohlc, [:Open′, :High′, :Low′, :Close′]) |> first
1×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-03
│            │ Open′  │ High′ │ Low′   │ Close′ │
├────────────┼────────┼───────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │

julia> rename(ohlc, :Open => :Open′) |> first
1×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-03
│            │ Open′  │ High  │ Low    │ Close  │
├────────────┼────────┼───────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │

julia> rename(ohlc, :Open => :Open′, :Close => :Close′) |> first
1×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-03
│            │ Open′  │ High  │ Low    │ Close′ │
├────────────┼────────┼───────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │

julia> rename(ohlc, Dict(:Open => :Open′, :Close => :Close′)...) |> first
1×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-03
│            │ Open′  │ High  │ Low    │ Close′ │
├────────────┼────────┼───────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │

julia> rename(Symbol ∘ uppercase ∘ string, ohlc) |> first
1×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-03
│            │ OPEN   │ HIGH  │ LOW    │ CLOSE  │
├────────────┼────────┼───────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │

julia> rename(uppercase, ohlc, String) |> first
1×4 TimeArray{Float64,2,Date,Array{Float64,2}} 2000-01-03 to 2000-01-03
│            │ OPEN   │ HIGH  │ LOW    │ CLOSE  │
├────────────┼────────┼───────┼────────┼────────┤
│ 2000-01-03 │ 104.88 │ 112.5 │ 101.69 │ 111.94 │
TimeSeries.renameFunction.
rename(ta::TimeArray, colnames::Vector{Symbol})
rename(ta::TimeArray, colname::Symbol)
rename(ta::TimeArray, orig => new, ...)
rename(f::Base.Callable, ta, colnametyp)

Rename the columns of a TimeArray.

See also rename!.

Arguments

  • colnametyp is the input type for the function f. The valid value is Symbol or String.
source
TimeSeries.rename!Function.
rename!(ta::TimeArray, colnames::Vector{Symbol})
rename!(ta::TimeArray, colname::Symbol)
rename!(ta::TimeArray, orig => new, ...)
rename!(f::Base.Callable, ta, colnametyp)

In-place rename the columns of a TimeArray.

See also rename.

Arguments

  • colnametyp is the input type for the function f. The valid value is Symbol or String.
source