2
$\begingroup$

My goal is to combine two time series with common properties and preserve those properties.

Say, we have two monthly series:

dt1 = DateRange[{2001, 1}, {2010, 1}, {1, "Month"}];
dt2 = DateRange[{2001, 2}, {2010, 2}, {1, "Month"}];

v = RandomReal[{0, 1}, Length@dt1]

ts1 = TimeSeries[Transpose[{dt1, v}]]
ts2 = TimeSeries[Transpose[{dt2, v}]]

Both ts1 and ts2 are constructed as Regular: True and Minimum increment: {1, Month}.

enter image description here

However, when I insert one into another:

TimeSeriesInsert[ts1, ts2]

The resulting series is Regular: False and Minimum increment: {28, Days}.

enter image description here

What is the recommended approach to combining two time series into one while preserving their regularity properties?

ps My current (silly) solution takes the TimeSeries objects apart and then reassembles them:

TimeSeriesInsertPreserve[ts1_, ts2_] := Module[
{d1 = ts2["Dates"],
  d2 = Complement[ts1["Dates"], ts2["Dates"]]},
  TimeSeries[
   Sort[
Join[
 {#, ts1@#} & /@ d2,
 ts2["DatePath"]
 ]]]]
$\endgroup$
  • 1
    $\begingroup$ When you combine the series, what is supposed to happen with the values (v)? reference.wolfram.com/language/ref/TimeSeriesInsert.html replaces v at t when a new {t,v] is inserted. It seems like TimeSeriesInsert is more geared towards straightforward "editing" of a time series. $\endgroup$ – FredrikD Feb 14 at 7:41
  • $\begingroup$ @FredrikD, yes, the behavior of TimeSeriesInsert with regards to values is not a problem. My problem is that it changes the properties of the TimeSeries object from that with regular spacing of observations to irregular time series. $\endgroup$ – iav Feb 14 at 9:50
  • $\begingroup$ I've added screens - hopefully this makes the problem clearer. $\endgroup$ – iav Feb 14 at 9:53
  • 1
    $\begingroup$ noted. another specific workaround is TimeSeriesResample[TimeSeriesInsert[ts1, ts2], "Month"] $\endgroup$ – Gosia Mar 27 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.