Skip to content

Commit

Permalink
Convert ta to df
Browse files Browse the repository at this point in the history
  • Loading branch information
femtotrader committed Oct 16, 2016
1 parent 8afa571 commit 3dc52f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

[![codecov.io](http://codecov.io/github/femtotrader/TimeSeriesReader.jl/coverage.svg?branch=master)](http://codecov.io/github/femtotrader/TimeSeriesReader.jl?branch=master)

Convert DataFrame (from [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl)) to TimeArray (from [TimeSeries.jl](https://github.com/JuliaStats/TimeSeries.jl))
- Convert DataFrame (from [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl)) to TimeArray (from [TimeSeries.jl](https://github.com/JuliaStats/TimeSeries.jl))

- Convert TimeArray (from [TimeSeries.jl](https://github.com/JuliaStats/TimeSeries.jl)) tp DataFrame (from [DataFrames.jl](https://github.com/JuliaStats/DataFrames.jl))
10 changes: 9 additions & 1 deletion src/TimeSeriesReader.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TimeSeriesReader

# package code goes here
export to_TimeArray, basepath
export to_TimeArray, to_DataFrame, basepath

using DataFrames
using TimeSeries
Expand All @@ -19,4 +19,12 @@ function to_TimeArray(df::DataFrame; timestamp=:Date, colnames=Symbol[])
ta
end

function to_DataFrame(ta::TimeArray)
df = DataFrame(hcat(ta.timestamp, ta.values))
colnames = [Symbol(s) for s in ta.colnames]
colnames = vcat(:Date, colnames)
names!(df, colnames)
df
end

end # module
6 changes: 5 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ ta = to_TimeArray(df, colnames=[:Open, :High, :Low, :Close])
# Test convert TimeArray to DataFrame
using TimeSeries
filename = joinpath(basepath(), "ford_2012.csv")
ohlcv = readtimearray(filename)
ta2 = readtimearray(filename)
ta2 = ta2["Open", "High", "Low", "Close"]
df2 = to_DataFrame(ta2)
@test names(df2) == [:Date, :Open, :High, :Low, :Close]
println(df2)

0 comments on commit 3dc52f9

Please sign in to comment.