Tables.jl Interface Integration

Quoted from the home page of Tables.jl:

The Table.jl package provides simple, yet powerful interface functions for working with all kinds tabular data through predictable access patterns.

The integration provides handy constructor to convert a table between several types. The time index of a TimeArray is considered as a normal data column named timestamp.

Here this doc shows some example usages of this integration. Converting table between DataFrames or CSV are quite common cases.

eachrow and eachcol iterators

In Julia v1.1+, these two functions are supported and baked by Tables.jl.

julia> using MarketData
julia> for row ∈ eachrow(ohlc) time = row.timestamp c = row.Close # ... end

TimeArray to DataFrame

julia> using MarketData, DataFrames
julia> df = DataFrame(ohlc)500×5 DataFrame Row │ timestamp Open High Low Close │ Date Float64 Float64 Float64 Float64 ─────┼──────────────────────────────────────────────── 1 │ 2000-01-03 104.88 112.5 101.69 111.94 2 │ 2000-01-04 108.25 110.62 101.19 102.5 3 │ 2000-01-05 103.75 110.56 103.0 104.0 4 │ 2000-01-06 106.12 107.0 95.0 95.0 5 │ 2000-01-07 96.5 101.0 95.5 99.5 6 │ 2000-01-10 102.0 102.25 94.75 97.75 7 │ 2000-01-11 95.94 99.38 90.5 92.75 8 │ 2000-01-12 95.0 95.5 86.5 87.19 ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ 494 │ 2001-12-20 21.4 21.47 20.62 20.67 495 │ 2001-12-21 21.01 21.54 20.8 21.0 496 │ 2001-12-24 20.9 21.45 20.9 21.36 497 │ 2001-12-26 21.35 22.3 21.14 21.49 498 │ 2001-12-27 21.58 22.25 21.58 22.07 499 │ 2001-12-28 21.97 23.0 21.96 22.43 500 │ 2001-12-31 22.51 22.66 21.83 21.9 485 rows omitted

DataFrame to TimeArray

In this case, user needs to point out the column of time index via the timestamp keyword argument.

julia> df′ = DataFrames.rename(df, :timestamp => :A);
julia> df′ |> firstDataFrameRow Row │ A Open High Low Close │ Date Float64 Float64 Float64 Float64 ─────┼──────────────────────────────────────────────── 1 │ 2000-01-03 104.88 112.5 101.69 111.94
julia> TimeArray(df′, timestamp = :A)500×4 TimeArray{Float64, 2, Date, Matrix{Float64}} 2000-01-03 to 2001-12-31 ┌────────────┬────────┬────────┬────────┬────────┐ │ │ 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 │ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ ⋮ │ │ 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 │ └────────────┴────────┴────────┴────────┴────────┘ 485 rows omitted

Save a TimeArray via CSV.jl

using CSV
CSV.write(filename, ta)

Load a TimeArray from csv file via CSV.jl

using CSV
TimeArray(CSV.File(filename), timestamp = :timestamp)