Loading CSV file to TimeSeries data

Hi, I am trying to load some OHLC data into TimeArray (from TimeSeries.jl).

The documentation (Tables.jl Interface Integration · TimeSeries.jl) says I can load it directly as

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

But when I try the following code, I get error.

using TimeSeries
using CSV
filename = "./ohlc-data.csv"
data = TimeArray(CSV.File(filename), timestamp = :time)

ERROR:

MethodError: no method matching TimeArray(::Vector{Int64}, ::Matrix{Float64}, ::Vector{Symbol}, ::CSV.File; unchecked=false)
Closest candidates are:
  TimeArray(::AbstractVector{D}, ::AbstractArray{T, N}, ::Vector{Symbol}, ::Any; args...) where {T, N, D<:TimeType} at ~/.julia/packages/TimeSeries/e3V5T/src/timearray.jl:91
  TimeArray(::D, ::AbstractArray{T, N}, ::Vector{Symbol}, ::Any; args...) where {T, N, D<:TimeType} at ~/.julia/packages/TimeSeries/e3V5T/src/timearray.jl:96
  TimeArray(::AbstractVector{D}, ::AbstractArray{T, N}, ::Vector{S}, ::Any; args...) where {T, N, D<:TimeType, S<:AbstractString} at deprecated.jl:70
  ...

Stacktrace:
 [1] TimeArray(x::CSV.File; timestamp::Symbol, timeparser::Function, unchecked::Bool)
   @ TimeSeries.TablesIntegration ~/.julia/packages/TimeSeries/e3V5T/src/tables.jl:82
 [2] top-level scope
   @ In[6]:4
 [3] eval
   @ ./boot.jl:360 [inlined]
 [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

My ohlc data file is of format

time,low,high,open,close,volume
<unixtimestamp>,<float>,<float>,<float>,<float>,<float>
...

How can I fix this error?

I experienced the same error when trying to do this. A quick fix would be to just load to a DataFrame first…

From what I can tell, the TimeArray constructor requires the timestamp column to be some kind of TimeType, so like Dates or DateTimes, but in your case, you have a Vector{Int} (from the backtrace). And from your file, it looks like your time column is <unixtimestamp>, so you’ll want to convert. It looks like the constructor takes a “converter” function, so I think this should work for you:

data = TimeArray(CSV.File(filename), timestamp = :time, timeparser = Dates.unix2datetime)

Otherwise, I’d open an issue at the TimeSeries.jl/tables.jl at master · JuliaStats/TimeSeries.jl · GitHub package and get some direct help there.

1 Like

Thanks a lot. Adding timeparser worked! I was having unixtimestamp as the first column and adding the parser helped it to load. :grinning: