3

I have a julia TimeArray, let's say ta, and I want to build sub_array a TimeArray sub_ta by extracting some of the columns. Some month ago, I used a code similar to minimal example below, but which doesn't work anymore

import TimeSeries
import Dates
dates_index = [ Dates.Date(1970,1,day) for day in [1,2,3,4,5] ]
values = [ [1.0  2.0  3.0  4.0  5.0] ; [10.0  20.0  30.0  40.0  50.0] ; [ 100.0 200.0 300.0 400.0 500.0] ]
ta = TimeSeries.TimeArray( dates_index, transpose(values), [ :col1, :col2, :col3 ] )
sub_ta = ta[ [ :col1 , :col2 ] ]

ERROR: MethodError: no method matching getindex(::TimeSeries.TimeArray{Float64,2,Dates.Date,LinearAlgebra.Transpose{Float64,Array{Float64,2}}}, ::Array{Symbol,1})
Closest candidates are:
  getindex(::TimeSeries.TimeArray, ::Integer) at /home/guilhem/.julia/packages/TimeSeries/bbwst/src/timearray.jl:259
  getindex(::TimeSeries.TimeArray, ::UnitRange{#s30} where #s30<:Integer) at /home/guilhem/.julia/packages/TimeSeries/bbwst/src/timearray.jl:268
  getindex(::TimeSeries.TimeArray, ::AbstractArray{#s30,1} where #s30<:Integer) at /home/guilhem/.julia/packages/TimeSeries/bbwst/src/timearray.jl:276

What seems strange to me is that there is, in the source of the TimeSeries library (in the file timearray.jl) a function getindex which should work if we want to work on many columns.

# array of columns by name
function getindex(ta::TimeArray, ss::Symbol...)
ns = [findcol(ta, s) for s in ss]
TimeArray(timestamp(ta), values(ta)[:, ns], collect(ss), meta(ta))  
end

But I think I didn't get the proper way to use it, probably due to the splat operator what I don't really master

problem is both on julia-1.1.0 and julia-1.3.1, with TimeSeries v0.14.0

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Guilhem L.
  • 421
  • 2
  • 8

2 Answers2

3

Finally I think I found the solution, I was quite close :

sub_ta = ta[ [:col1 , col2]...]

The best introduction I found on the ..., the splat operator is on this page (search "..." or "splat") : enter link description here

Guilhem L.
  • 421
  • 2
  • 8
2

What version of TimeSeries are you using?

(tmp) pkg> status
    Status `/tmp/Project.toml`
  [9e3dc215] TimeSeries v0.16.1

In version 0.16.1, both syntaxes that you mention seem to work:

julia> ta
5×3 TimeSeries.TimeArray{Float64,2,Dates.Date,LinearAlgebra.Transpose{Float64,Array{Float64,2}}} 1970-01-01 to 1970-01-05
│            │ col1  │ col2  │ col3  │
├────────────┼───────┼───────┼───────┤
│ 1970-01-01 │ 1.0   │ 10.0  │ 100.0 │
│ 1970-01-02 │ 2.0   │ 20.0  │ 200.0 │
│ 1970-01-03 │ 3.0   │ 30.0  │ 300.0 │
│ 1970-01-04 │ 4.0   │ 40.0  │ 400.0 │
│ 1970-01-05 │ 5.0   │ 50.0  │ 500.0 │

julia> ta[[:col1, :col2]]
5×2 TimeSeries.TimeArray{Float64,2,Dates.Date,Array{Float64,2}} 1970-01-01 to 1970-01-05
│            │ col1  │ col2  │
├────────────┼───────┼───────┤
│ 1970-01-01 │ 1.0   │ 10.0  │
│ 1970-01-02 │ 2.0   │ 20.0  │
│ 1970-01-03 │ 3.0   │ 30.0  │
│ 1970-01-04 │ 4.0   │ 40.0  │
│ 1970-01-05 │ 5.0   │ 50.0  │

julia> ta[[:col1, :col2]...]
5×2 TimeSeries.TimeArray{Float64,2,Dates.Date,Array{Float64,2}} 1970-01-01 to 1970-01-05
│            │ col1  │ col2  │
├────────────┼───────┼───────┤
│ 1970-01-01 │ 1.0   │ 10.0  │
│ 1970-01-02 │ 2.0   │ 20.0  │
│ 1970-01-03 │ 3.0   │ 30.0  │
│ 1970-01-04 │ 4.0   │ 40.0  │
│ 1970-01-05 │ 5.0   │ 50.0  │

Note that this last version is a rather convoluted way of writing ta[:col1, :col2]:

julia> ta[:col1, :col2]
5×2 TimeSeries.TimeArray{Float64,2,Dates.Date,Array{Float64,2}} 1970-01-01 to 1970-01-05
│            │ col1  │ col2  │
├────────────┼───────┼───────┤
│ 1970-01-01 │ 1.0   │ 10.0  │
│ 1970-01-02 │ 2.0   │ 20.0  │
│ 1970-01-03 │ 3.0   │ 30.0  │
│ 1970-01-04 │ 4.0   │ 40.0  │
│ 1970-01-05 │ 5.0   │ 50.0  │
François Févotte
  • 19,520
  • 4
  • 51
  • 74