6
$\begingroup$

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.

$\endgroup$

4 Answers 4

7
$\begingroup$

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}}
$\endgroup$
6
$\begingroup$

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}}

$\endgroup$
5
$\begingroup$

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}}

$\endgroup$
1
$\begingroup$

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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.