Threading data on TimeSeries

5

2

Say I have two TimeSeries:

x = TimeSeries[{2, 4, 1, 10}, {{1, 2, 4, 5}}]
y = TimeSeries[{6, 2, 6, 3, 9}, {{1, 2, 3, 4, 5}}]

x has a value at times: 1,2,4,5

y has a value at times: 1,2,3,4,5

I would like to build a list of pairs {$x_i$, $y_i$} which would not include missing elements (in this case element x element at time 3 is missing)

The desired result would be:

{{2,6}, {4,2}, {1,3}, {10,9}}

I have a feeling that this should be simple and perhaps I'm not using right tools.

maxgo2

Posted 2019-06-13T10:56:52.197

Reputation: 135

Answers

6

TimeSeriesThread is probably the tool for the job when you specify the right options:

x = TimeSeries[{2, 4, 1, 10}, {{1, 2, 4, 5}}];
y = TimeSeries[{6, 2, 6, 3, 9}, {{1, 2, 3, 4, 5}}];
DeleteMissing[
 TimeSeriesThread[Identity, {x, y}, ResamplingMethod -> None]["Values"],
 1, 1
]

{{2, 6}, {4, 2}, {1, 3}, {10, 9}}

Sjoerd Smit

Posted 2019-06-13T10:56:52.197

Reputation: 15 418

6

One possible solution is to use TimeSeriesResample:

td = TimeSeriesResample[TemporalData[{x, y}], "Intersection"];

Using "Intersection" instructs TimeSeriesResample to use only common timestamps for all paths.

Then

td["Paths"] // (Part[#, All, All, -1] &) /* Transpose

evaluates to

{{2, 6}, {4, 2}, {1, 3}, {10, 9}}

user42582

Posted 2019-06-13T10:56:52.197

Reputation: 3 930

5

One way you can do this is by combining the two TimeSeries into TemporalData with no resampling:

x = TimeSeries[{2, 4, 1, 10}, {{1, 2, 4, 5}}]
y = TimeSeries[{6, 2, 6, 3, 9}, {{1, 2, 3, 4, 5}}]

td = TemporalData[{x, y}, ResamplingMethod -> None]

Then, we can get the "Slices" for the range of times:

td["SliceData", Range[5]]

{{2, 4, Missing[], 1, 10}, {6, 2, 6, 3, 9}}

And we can Transpose that to get pairs:

Transpose@td["SliceData", Range[5]]

{{2, 6}, {4, 2}, {Missing[], 6}, {1, 3}, {10, 9}}

and finally, drop all pairs that contain Missing using DeleteMissing's second and third argument:

DeleteMissing[Transpose@td["SliceData", Range[5]], 1, 1]

{{2, 6}, {4, 2}, {1, 3}, {10, 9}}

Carl Lange

Posted 2019-06-13T10:56:52.197

Reputation: 10 514

1

You can simply normalize the data (x and y), convert it to an Association and later Merge it.

data = {<|Rule @@@ Normal[x]|>, <|Rule @@@ Normal[y]|>};
Merge[
      data, 
      If[Length[#] == 2, #, Nothing] &
    ] // Values

{{2, 6}, {4, 2}, {1, 3}, {10, 9}}

Anjan Kumar

Posted 2019-06-13T10:56:52.197

Reputation: 4 551