Function Repository Resource:

TimeSeriesPad

Source Notebook

Make a time series in full periods of specified granularity by padding with Missing

Contributed by: Ting Sun

ResourceFunction["TimeSeriesPad"][ts,gran]

makes ts in full periods of gran by padding Missing[] on both sides wherever necessary.

ResourceFunction["TimeSeriesPad"][ts,gran,pad]

pads with element pad.

Details

ResourceFunction["TimeSeriesPad"] uses DateObject under the hood to determine time stamps to pad extra elements onto, so it accepts the same granularity as in DateBounds.

Examples

Basic Examples (1) 

Extend a time series into a whole month by padding Missing[] at both ends:

In[1]:=
ts = TimeSeries[Table[{{2012, 1, d}, RandomReal[10]}, {d, 10, 16}]];
tsPad = ResourceFunction["TimeSeriesPad"][ts, "Month"];
TimelinePlot[{ts, tsPad}]
Out[3]=

Scope (1) 

Pad a time series with a customised padding element:

In[4]:=
ts = TimeSeries[Table[{{2012, 1, d}, RandomReal[10]}, {d, 10, 16}]];
tsPadCustom = ResourceFunction["TimeSeriesPad"][ts, "Month", x]
Out[4]=

Applications (1) 

TimeSeriesPad may assist partitioning using natural temporal granules with "nice" bounds:

In[5]:=
ts = TimeSeries[
   Table[{{2012, 1, d + RandomReal[]}, RandomReal[10]}, {d, 23, 36}]];
{ResourceFunction["TimeSeriesPartition"][ts, "Month"], Most@ResourceFunction["TimeSeriesPartition"][
   ResourceFunction["TimeSeriesPad"][ts, "Month"], "Month"]}
Out[6]=

Possible Issues (2) 

The padded time series may have a different minimal time increment due to inclusion of ending time stamps of specified temporal granule:

In[7]:=
ts = TimeSeries[
   Table[{{2012, 1, d + RandomReal[]}, RandomReal[10]}, {d, 10, 16}]];
tsPad = ResourceFunction["TimeSeriesPad"][ts, "Week"];
MinimumTimeIncrement@ts === MinimumTimeIncrement@tsPad
Out[8]=

However, except for the bounding time stamps, TimeSeriesPad respects the minimum time increment of the original time series to pad:

In[9]:=
Min@Differences@Rest@Most@tsPad["Dates"] == Quantity @@ MinimumTimeIncrement@ts
Out[9]=

Publisher

Ting Sun

Version History

  • 1.0.0 – 04 October 2021

Related Resources

License Information