Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds method for splitting data into periods #528

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/src/split.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,14 @@ using MarketData
tail(cl)
tail(cl, 3)
```

## Splitting by period

Splitting data by a given function, e.g. `Dates.day` into periods.

```@repl
using TimeSeries
using MarketData

split(cl, Dates.day)
```
12 changes: 6 additions & 6 deletions src/TimeSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ using Tables
using PrettyTables: pretty_table

export TimeArray, AbstractTimeSeries,
when, from, to, findwhen, timestamp, values, colnames, meta, head, tail,
lag, lead, diff, percentchange, moving, upto,
uniformspaced, uniformspace, dropnan,
basecall,
merge, collapse,
readtimearray, writetimearray
when, from, to, findwhen, timestamp, values, colnames, meta, head, tail, split,
lag, lead, diff, percentchange, moving, upto,
uniformspaced, uniformspace, dropnan,
basecall,
merge, collapse,
readtimearray, writetimearray

# modify.jl
export rename, rename!
Expand Down
52 changes: 43 additions & 9 deletions src/split.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ when(ta::TimeArray, period::Function, t::String) =

# from, to ######################

from(ta::TimeArray{T, N, D}, d::D) where {T, N, D} =
from(ta::TimeArray{T,N,D}, d::D) where {T,N,D} =
length(ta) == 0 ? ta :
d < timestamp(ta)[1] ? ta :
d > timestamp(ta)[end] ? ta[1:0] :
ta[searchsortedfirst(timestamp(ta), d):end]
d < timestamp(ta)[1] ? ta :
d > timestamp(ta)[end] ? ta[1:0] :
ta[searchsortedfirst(timestamp(ta), d):end]

to(ta::TimeArray{T, N, D}, d::D) where {T, N, D} =
to(ta::TimeArray{T,N,D}, d::D) where {T,N,D} =
length(ta) == 0 ? ta :
d < timestamp(ta)[1] ? ta[1:0] :
d > timestamp(ta)[end] ? ta :
ta[1:searchsortedlast(timestamp(ta), d)]
d < timestamp(ta)[1] ? ta[1:0] :
d > timestamp(ta)[end] ? ta :
ta[1:searchsortedlast(timestamp(ta), d)]

###### findall ##################

Expand All @@ -43,7 +43,7 @@ findwhen(ta::TimeArray{Bool,1}) = timestamp(ta)[findall(values(ta))]
end
end

@generated function tail(ta::TimeArray{T,N}, n::Int=6) where {T,N}
@generated function tail(ta::TimeArray{T,N}, n::Int=6) where {T,N}
new_values = (N == 1) ? :(values(ta)[start:end]) : :(values(ta)[start:end, :])

quote
Expand All @@ -58,3 +58,37 @@ end
Base.first(ta::TimeArray) = head(ta, 1)

Base.last(ta::TimeArray) = tail(ta, 1)


"""
split(data::TimeSeries.TimeArray, period::Function)

Split `data` by `period` function, returns a vector of `TimeSeries.TimeArray`.

## Arguments

- `data::TimeSeries.TimeArray`: Data to split
- `period::Function`: Function, e.g. `Dates.day` that is used to split the `data`.
"""
split(data::TimeSeries.TimeArray, period::Function) = map(i -> data[i], _split(data, period))
function _split(data::TimeSeries.TimeArray, period::Function)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, I'm think about : does return an iterator better than just an Array?

isempty(data) && return data

m = length(data)
ts = TimeSeries.timestamp(data)
idx = UnitRange{Int}[]
sizehint!(idx, m)

t0 = period(ts[1])
j = 1
for i in 1:(m-1)
t1 = period(ts[i+1])
t0 == t1 && continue
push!(idx, j:i)
j = i + 1
t0 = t1
end
push!(idx, j:m)

return idx
end
Loading