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 retime #529

Open
wants to merge 6 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
3 changes: 1 addition & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ jobs:
fail-fast: false
matrix:
version:
- "1.6.7" # LTS
- "1.6"
- "1.10.7" # LTS
- "1" # Latest Release
os:
- ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.swp
docs/build/
Manifest.toml
test/Manifest.toml
11 changes: 0 additions & 11 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
[compat]
DelimitedFiles = "1"
DocStringExtensions = "0.8, 0.9"
MarketData = "0.14"
RecipesBase = "0.5, 0.7, 0.8, 1.0"
Reexport = "1"
Statistics = "1"
Expand All @@ -27,13 +26,3 @@ julia = "1.6"
PrettyTables = "2"
IteratorInterfaceExtensions = "1"
TableTraits = "1"

[extras]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
MarketData = "945b72a4-3b13-509d-9b46-1525bb5c06de"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["CSV", "DataFrames", "MarketData", "Random", "Test"]
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ makedocs(;
"tables.md",
"dotfile.md",
"plotting.md",
"retime.md",
],
)

Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Pages = [
"tables.md",
"dotfile.md",
"plotting.md",
"retime.md",
]
```
6 changes: 2 additions & 4 deletions docs/src/plotting.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ ta = yahoo(:GOOG, YahooOpt(; period1=now() - Month(1)))

## Plotting as multiple series

The recipe allows `TimeArray` objects to be passed as input to `plot`. The
recipe will plot each variable as an individual line, aligning all
variables to the same y axis.
backend).
The recipe allows `TimeArray` objects to be passed as input to `plot`.
The recipe will plot each variable as an individual line, aligning all variables to the same y axis.

```@example plot
plot(ta[:Open, :High, :Low, :Close])
Expand Down
90 changes: 90 additions & 0 deletions docs/src/retime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Retime

The `retime` function allows you to retime, i.e. change the timestamps of a `TimeArray`, similar to what [Matlab's retime](https://www.mathworks.com/help/matlab/ref/timetable.retime.html) does.

```@example retime
using Plots, Dates, TimeSeries
default(show = false) # hide
ENV["GKSwstype"] = "100" # hide
gr()
timestamps = range(DateTime(2020, 1, 1), length = 7*24, step = Hour(1))
ta = TimeArray(timestamps, cumsum(randn(7*24)), [:a])
```

## Using a new time step
```@example retime
retime(ta, Minute(15))
```

## Using new timestep vector
```@example retime
new_timestamps = range(DateTime(2020, 1, 1), DateTime(2020, 1, 2), step = Minute(15))
retime(ta, new_timestamps)
```

## Irregular timestamps
You can perform retime on irregularly spaced timestamps, both using a `TimeArray` with irregular timestamps or using a vector of irregular timestamps. Depending on the timestamps `upsampling` or `downsampling` is used.
```@example retime
new_timestamps = vcat(
range(DateTime(2020, 1, 1), DateTime(2020, 1, 2)-Minute(15), step = Minute(15)),
range(DateTime(2020, 1, 2), DateTime(2020, 1, 3), step = Hour(1)),
)
retime(ta, new_timestamps)
```

## Upsampling

Interpolation is done using the `upsample` argument. If no data is directly hit, the specified `upsample` method is used. Available `upsample` methods are:
- `Linear()` or `:linear`
- `Nearest()` or `:nearest`
- `Previous()` or `:previous`
- `Next()` or `:next`

```@example retime
ta_ = retime(ta, Minute(15), upsample=Linear())
```

```@example retime
plot(ta)
plot!(ta_)
savefig("retime-upsampling.svg"); nothing # hide
```
![](retime-upsampling.svg)

## Downsampling

Downsampling or aggregation is done using the `downsample` argument. This applies a function to each interval not including the right-edge of the interval. If no data is present in the interval the specified `upsample` method is used.
Available `downsample` methods are:
- `Mean()` or `:mean`
- `Min()` or `:min`
- `Max()` or `:max`
- `Count()` or `:count`
- `Sum()` or `:sum`
- `Median()` or `:median`
- `First()` or `:first`
- `Last()` or `:last`

```@example retime
ta_ = retime(ta, Hour(6), downsample=Mean())
```

```@example retime
plot(ta)
plot!(ta_)
savefig("retime-downsample.svg"); nothing # hide
```
![](retime-downsample.svg)

## Extrapolation

Extrapolation at the beginning and end of the time series is done using the `extrapolate` argument.
Available `extrapolate` methods are:
- `FillConstant(value)` or `:fillconstant`
- `NearestExtrapolate()` or `:nearest`
- `MissingExtrapolate()` or `:missing`
- `NaNExtrapolate()` or `:nan`

```@example retime
new_timestamps = range(DateTime(2019, 12, 31), DateTime(2020, 1, 2), step = Minute(15))
ta_ = retime(ta, new_timestamps, extrapolate=MissingExtrapolate())
```
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)
```
20 changes: 19 additions & 1 deletion src/TimeSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,24 @@ export TimeArray,
merge,
collapse,
readtimearray,
writetimearray
writetimearray,
retime,
Linear,
Previous,
Next,
Nearest,
Mean,
Min,
Max,
Count,
Sum,
Median,
First,
Last,
FillConstant,
NearestExtrapolate,
MissingExtrapolate,
NaNExtrapolate

# modify.jl
export rename, rename!
Expand All @@ -58,5 +75,6 @@ include("modify.jl")
include("basemisc.jl")
include("deprecated.jl")
include("plotrecipes.jl")
include("retime.jl")

end # module TimeSeries
Loading
Loading