-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
351 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
[deps] | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" | ||
MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de" | ||
TimeSeries = "9e3dc215-6440-5c97-bce1-76c03772f85e" | ||
|
||
[compat] | ||
Documenter = "~0.20" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ makedocs( | |
"apply.md", | ||
"combine.md", | ||
"readwrite.md", | ||
"tables.md", | ||
"dotfile.md", | ||
"plotting.md", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ Pages = [ | |
"apply.md", | ||
"combine.md", | ||
"readwrite.md", | ||
"tables.md", | ||
"dotfile.md", | ||
"plotting.md", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Tables.jl Interface Integration | ||
|
||
Quoted from the home page of Tables.jl: | ||
> The [Table.jl](https://github.com/JuliaData/Tables.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 `DataFrame`s or `CSV` are quite common cases. | ||
|
||
|
||
## `TimeArray` to `DataFrame` | ||
|
||
```@repl df-ta | ||
using MarketData, DataFrames | ||
df = DataFrame(ohlc) | ||
``` | ||
|
||
## `DataFrame` to `TimeArray` | ||
|
||
In this case, user needs to point out the column of time index via the | ||
`timestamp` keyword argument. | ||
|
||
```@repl df-ta | ||
df′ = DataFrames.rename(df, :timestamp => :A); | ||
df′ |> first | ||
TimeArray(df′, timestamp = :A) | ||
``` | ||
|
||
|
||
## Save a `TimeArray` via `CSV.jl` | ||
|
||
```julia | ||
using CSV | ||
CSV.write(filename, ta) | ||
``` | ||
|
||
|
||
## Load a `TimeArray` from csv file via `CSV.jl` | ||
|
||
```julia | ||
using CSV | ||
TimeArray(CSV.File(filename), timestamp = :timestamp) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# JuliaData/Tables.jl integration | ||
|
||
# const TimeArrayColIter = Tables.EachColumn{<:TimeArray} | ||
module TablesIntegration | ||
|
||
using ..TimeSeries | ||
|
||
using Tables | ||
|
||
|
||
# S: the column names, including the extra column name for time index | ||
# N: number of rows | ||
# M: number of columns | ||
struct TableIter{T<:AbstractTimeSeries,S,N,M} | ||
x::T | ||
end | ||
|
||
function TableIter(ta::T) where {T<:TimeArray} | ||
N, M = size(ta, 1), size(ta, 2) + 1 | ||
S = Tuple(TimeSeries.replace_dupes!([:timestamp; colnames(ta)])) | ||
TableIter{T,S,N,M}(ta) | ||
end | ||
|
||
data(i::TableIter) = getfield(i, :x) | ||
|
||
TimeSeries.timestamp(i::TableIter) = timestamp(data(i)) | ||
TimeSeries.colnames(i::TableIter) = colnames(data(i)) | ||
|
||
Base.getindex(x::TableIter{<:TimeArray}, i::Integer) = | ||
i == 1 ? timestamp(x) : values(data(x)[colnames(x)[i - 1]]) | ||
Base.getindex(x::TableIter{<:TimeArray}, j::Integer, i::Integer) = | ||
i == 1 ? timestamp(x)[j] : values(data(x)[colnames(x)[i - 1]])[j] | ||
|
||
Base.length(::TableIter{<:TimeArray,S,N,M}) where {S,N,M} = M | ||
Base.lastindex(::TableIter{<:TimeArray,S,N,M}) where {S,N,M} = M | ||
Base.lastindex(::TableIter{<:TimeArray,S,N,M}, d::Integer) where {S,N,M} = | ||
ifelse(d == 1, N, ifelse(d == 2, M, 1)) | ||
Base.size(::TableIter{<:TimeArray,S,N,M}) where {S,N,M} = (N, M) | ||
|
||
Base.propertynames(x::TableIter{<:TimeArray,S}) where {S} = S | ||
Base.getproperty(x::TableIter{<:TimeArray,S}, c::Symbol) where {S} = | ||
c == S[1] ? timestamp(x) : values(getproperty(data(x), c)) | ||
|
||
function Base.iterate(x::TableIter, i::Integer = 1) | ||
i > length(x) && return nothing | ||
x[i], i + 1 | ||
end | ||
|
||
Tables.istable(::Type{<:AbstractTimeSeries}) = true | ||
Tables.rowaccess(::Type{<:TimeArray}) = true | ||
Tables.rows(ta::TimeArray) = Tables.rows(Tables.columntable(ta)) | ||
Tables.columnaccess(::Type{<:TimeArray}) = true | ||
Tables.columns(ta::TimeArray) = TableIter(ta) | ||
Tables.eachcolumn(i::TableIter) = i | ||
Tables.schema(ta::AbstractTimeSeries{T,N,D}) where {T,N,D} = Tables.schema(TableIter(ta)) | ||
Tables.schema(i::TableIter{T,S}) where {T,S} = Tables.Schema(S, coltypes(data(i))) | ||
|
||
coltypes(x::AbstractTimeSeries{T,N,D}) where {T,N,D} = (D, (T for _ ∈ 1:size(x, 2))...) | ||
|
||
function TimeSeries.TimeArray(x; timestamp::Symbol) | ||
Tables.istable(x) || throw(ArgumentError("TimeArray requires a table input")) | ||
|
||
sch = Tables.schema(x) | ||
names = sch.names | ||
(timestamp ∉ names) && throw(ArgumentError("time index `$timestamp` not found")) | ||
names′ = filter(!isequal(timestamp), collect(sch.names)) | ||
|
||
cols = Tables.columns(x) | ||
val = mapreduce(n -> collect(getproperty(cols, n)), hcat, names′) | ||
TimeArray(collect(getproperty(cols, timestamp)), val, names′, x) | ||
end | ||
|
||
end # TablesIntegration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ tests = [ | |
"readwrite", | ||
"timeseriesrc", | ||
"basemisc", | ||
"tables", | ||
] | ||
|
||
|
||
|
Oops, something went wrong.