Retime
The retime function allows you to retime, i.e. change the timestamps of a TimeArray, similar to what Matlab's retime does.
using Plots, Dates, TimeSeries
gr()
timestamps = range(DateTime(2020, 1, 1), length = 7*24, step = Hour(1))
ta = TimeArray(timestamps, cumsum(randn(7*24)), [:a])168×1 TimeArray{Float64, 1, DateTime, Vector{Float64}} 2020-01-01T00:00:00 to 2020-01-07T23:00:00
┌─────────────────────┬─────────┐
│ │ a │
├─────────────────────┼─────────┤
│ 2020-01-01T00:00:00 │ 2.31198 │
│ 2020-01-01T01:00:00 │ 3.87913 │
│ 2020-01-01T02:00:00 │ 3.28199 │
│ 2020-01-01T03:00:00 │ 2.2734 │
│ 2020-01-01T04:00:00 │ 2.58031 │
│ 2020-01-01T05:00:00 │ 2.95722 │
│ 2020-01-01T06:00:00 │ 2.87577 │
│ 2020-01-01T07:00:00 │ 4.00162 │
│ ⋮ │ ⋮ │
│ 2020-01-07T17:00:00 │ 7.71878 │
│ 2020-01-07T18:00:00 │ 8.16602 │
│ 2020-01-07T19:00:00 │ 10.0477 │
│ 2020-01-07T20:00:00 │ 9.26863 │
│ 2020-01-07T21:00:00 │ 9.72699 │
│ 2020-01-07T22:00:00 │ 9.93005 │
│ 2020-01-07T23:00:00 │ 11.3293 │
└─────────────────────┴─────────┘
153 rows omittedUsing a new time step
retime(ta, Minute(15))669×1 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2020-01-01T00:00:00 to 2020-01-07T23:00:00
┌─────────────────────┬─────────┐
│ │ a │
├─────────────────────┼─────────┤
│ 2020-01-01T00:00:00 │ 2.31198 │
│ 2020-01-01T00:15:00 │ 2.31198 │
│ 2020-01-01T00:30:00 │ 2.31198 │
│ 2020-01-01T00:45:00 │ 2.31198 │
│ 2020-01-01T01:00:00 │ 3.87913 │
│ 2020-01-01T01:15:00 │ 3.87913 │
│ 2020-01-01T01:30:00 │ 3.87913 │
│ 2020-01-01T01:45:00 │ 3.87913 │
│ ⋮ │ ⋮ │
│ 2020-01-07T21:30:00 │ 9.72699 │
│ 2020-01-07T21:45:00 │ 9.72699 │
│ 2020-01-07T22:00:00 │ 9.93005 │
│ 2020-01-07T22:15:00 │ 9.93005 │
│ 2020-01-07T22:30:00 │ 9.93005 │
│ 2020-01-07T22:45:00 │ 9.93005 │
│ 2020-01-07T23:00:00 │ 11.3293 │
└─────────────────────┴─────────┘
654 rows omittedUsing new timestep vector
new_timestamps = range(DateTime(2020, 1, 1), DateTime(2020, 1, 2), step = Minute(15))
retime(ta, new_timestamps)97×1 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2020-01-01T00:00:00 to 2020-01-02T00:00:00
┌─────────────────────┬───────────┐
│ │ a │
├─────────────────────┼───────────┤
│ 2020-01-01T00:00:00 │ 2.31198 │
│ 2020-01-01T00:15:00 │ 2.31198 │
│ 2020-01-01T00:30:00 │ 2.31198 │
│ 2020-01-01T00:45:00 │ 2.31198 │
│ 2020-01-01T01:00:00 │ 3.87913 │
│ 2020-01-01T01:15:00 │ 3.87913 │
│ 2020-01-01T01:30:00 │ 3.87913 │
│ 2020-01-01T01:45:00 │ 3.87913 │
│ ⋮ │ ⋮ │
│ 2020-01-01T22:30:00 │ 0.84457 │
│ 2020-01-01T22:45:00 │ 0.84457 │
│ 2020-01-01T23:00:00 │ -0.39346 │
│ 2020-01-01T23:15:00 │ -0.39346 │
│ 2020-01-01T23:30:00 │ -0.39346 │
│ 2020-01-01T23:45:00 │ -0.39346 │
│ 2020-01-02T00:00:00 │ -0.848762 │
└─────────────────────┴───────────┘
82 rows omittedIrregular 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.
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)121×1 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2020-01-01T00:00:00 to 2020-01-03T00:00:00
┌─────────────────────┬─────────┐
│ │ a │
├─────────────────────┼─────────┤
│ 2020-01-01T00:00:00 │ 2.31198 │
│ 2020-01-01T00:15:00 │ 2.31198 │
│ 2020-01-01T00:30:00 │ 2.31198 │
│ 2020-01-01T00:45:00 │ 2.31198 │
│ 2020-01-01T01:00:00 │ 3.87913 │
│ 2020-01-01T01:15:00 │ 3.87913 │
│ 2020-01-01T01:30:00 │ 3.87913 │
│ 2020-01-01T01:45:00 │ 3.87913 │
│ ⋮ │ ⋮ │
│ 2020-01-02T18:00:00 │ 1.81283 │
│ 2020-01-02T19:00:00 │ 1.91873 │
│ 2020-01-02T20:00:00 │ 2.60321 │
│ 2020-01-02T21:00:00 │ 3.91545 │
│ 2020-01-02T22:00:00 │ 3.05077 │
│ 2020-01-02T23:00:00 │ 2.98382 │
│ 2020-01-03T00:00:00 │ 2.56973 │
└─────────────────────┴─────────┘
106 rows omittedUpsampling
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:linearNearest()or:nearestPrevious()or:previousNext()or:next
ta_ = retime(ta, Minute(15), upsample=Linear())669×1 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2020-01-01T00:00:00 to 2020-01-07T23:00:00
┌─────────────────────┬─────────┐
│ │ a │
├─────────────────────┼─────────┤
│ 2020-01-01T00:00:00 │ 2.31198 │
│ 2020-01-01T00:15:00 │ 2.70377 │
│ 2020-01-01T00:30:00 │ 3.09555 │
│ 2020-01-01T00:45:00 │ 3.48734 │
│ 2020-01-01T01:00:00 │ 3.87913 │
│ 2020-01-01T01:15:00 │ 3.72984 │
│ 2020-01-01T01:30:00 │ 3.58056 │
│ 2020-01-01T01:45:00 │ 3.43128 │
│ ⋮ │ ⋮ │
│ 2020-01-07T21:30:00 │ 9.82852 │
│ 2020-01-07T21:45:00 │ 9.87929 │
│ 2020-01-07T22:00:00 │ 9.93005 │
│ 2020-01-07T22:15:00 │ 10.2799 │
│ 2020-01-07T22:30:00 │ 10.6297 │
│ 2020-01-07T22:45:00 │ 10.9795 │
│ 2020-01-07T23:00:00 │ 11.3293 │
└─────────────────────┴─────────┘
654 rows omittedplot(ta)
plot!(ta_)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:meanMin()or:minMax()or:maxCount()or:countSum()or:sumMedian()or:medianFirst()or:firstLast()or:last
ta_ = retime(ta, Hour(6), downsample=Mean())28×1 TimeArray{Float64, 2, DateTime, Matrix{Float64}} 2020-01-01T00:00:00 to 2020-01-07T18:00:00
┌─────────────────────┬───────────┐
│ │ a │
├─────────────────────┼───────────┤
│ 2020-01-01T00:00:00 │ 2.88067 │
│ 2020-01-01T06:00:00 │ 3.80897 │
│ 2020-01-01T12:00:00 │ 2.65148 │
│ 2020-01-01T18:00:00 │ 1.30297 │
│ 2020-01-02T00:00:00 │ -3.02952 │
│ 2020-01-02T06:00:00 │ -3.68937 │
│ 2020-01-02T12:00:00 │ -0.817812 │
│ 2020-01-02T18:00:00 │ 2.71414 │
│ ⋮ │ ⋮ │
│ 2020-01-06T06:00:00 │ 8.14749 │
│ 2020-01-06T12:00:00 │ 9.15137 │
│ 2020-01-06T18:00:00 │ 8.48118 │
│ 2020-01-07T00:00:00 │ 7.6436 │
│ 2020-01-07T06:00:00 │ 8.07861 │
│ 2020-01-07T12:00:00 │ 8.59339 │
│ 2020-01-07T18:00:00 │ 9.74477 │
└─────────────────────┴───────────┘
13 rows omittedplot(ta)
plot!(ta_)Extrapolation
Extrapolation at the beginning and end of the time series is done using the extrapolate argument. Available extrapolate methods are:
FillConstant(value)or:fillconstantNearestExtrapolate()or:nearestMissingExtrapolate()or:missingNaNExtrapolate()or:nan
new_timestamps = range(DateTime(2019, 12, 31), DateTime(2020, 1, 2), step = Minute(15))
ta_ = retime(ta, new_timestamps, extrapolate=MissingExtrapolate())193×1 TimeArray{Union{Missing, Float64}, 2, DateTime, Matrix{Union{Missing, Float64}}} 2019-12-31T00:00:00 to 2020-01-02T00:00:00
┌─────────────────────┬───────────┐
│ │ a │
├─────────────────────┼───────────┤
│ 2019-12-31T00:00:00 │ missing │
│ 2019-12-31T00:15:00 │ missing │
│ 2019-12-31T00:30:00 │ missing │
│ 2019-12-31T00:45:00 │ missing │
│ 2019-12-31T01:00:00 │ missing │
│ 2019-12-31T01:15:00 │ missing │
│ 2019-12-31T01:30:00 │ missing │
│ 2019-12-31T01:45:00 │ missing │
│ ⋮ │ ⋮ │
│ 2020-01-01T22:30:00 │ 0.84457 │
│ 2020-01-01T22:45:00 │ 0.84457 │
│ 2020-01-01T23:00:00 │ -0.39346 │
│ 2020-01-01T23:15:00 │ -0.39346 │
│ 2020-01-01T23:30:00 │ -0.39346 │
│ 2020-01-01T23:45:00 │ -0.39346 │
│ 2020-01-02T00:00:00 │ -0.848762 │
└─────────────────────┴───────────┘
178 rows omitted