## Remove a specific element of a TimeSeries?

8

1

If I have a TimeSeries, how do I remove an element if that element is another TimeSeries? Let's say I have:

ts2 = TimeSeries[{0.0, 0.0, 0.0, 0.0, 15.0, 0.0, 0.0, 0.0}, {1}];

and I want to remove the fifth (5) element. To find the peak, I can do:

FindPeaks[ts2] and that results in another TimeSeries. How to I use this information to remove element 5 so I have a TimeSeries that eliminates that peak? Resultant TimeSeries should be n-1 elements long, where n is the starting number of elements in the series.

1I don't think there is a way to do this apart from extracting the values from the TimeSeries, removing the peak and reconstructing the TimeSeries. Inefficient for large TimeSeries. – Rohit Namjoshi – 2020-03-19T02:59:33.003

7

You can use TimeSeriesResample as follows:

ts2 = TimeSeries[{0.0, 0.0, 0.0, 0.0, 15.0, 0.0, 0.0, 0.0}, {1}];
pks = FindPeaks[ts2];
ts3 = TimeSeriesResample[ts2, { Complement[ts2["Times"], pks["Times"]]}]

Normal@ts3


{{1, 0.}, {2, 0.}, {3, 0.}, {4, 0.}, {6, 0.}, {7, 0.}, {8, 0.}}

4

## Complete automation

I think what you are actually looking for is DeleteAnomalies. Define these data:

ts=TimeSeries[{1,2,3,15,3,2,1},{1}]


Now compare old and new:

DateListPlot[
{ts,TimeSeries[DeleteAnomalies[ts["Path"]]]},
PlotMarkers->Automatic,PlotRange->All]


## Surgery

Now if you want to play surgeon, you can define a function:

deleteTS[ts_,pts_]:=


It uses TimeSeriesInsert to replace elements at positions pts with Missing[] and then removes them with DeleteMissing. For example compare:

ts["Path"]
deleteTS[ts, {4}]["Path"]
deleteTS[ts, FindPeaks[ts]["Times"]]["Path"]

Out[1]= {{1, 1}, {2, 2}, {3, 3}, {4, 15}, {5, 3}, {6, 2}, {7, 1}}
Out[2]= {{1, 1}, {2, 2}, {3, 3}, {5, 3}, {6, 2}, {7, 1}}
Out[3]= {{1, 1}, {2, 2}, {3, 3}, {5, 3}, {6, 2}, {7, 1}}


You can also delete a set of points:

DateListPlot[
{ts,deleteTS[ts,{3,5}]},
PlotMarkers->Automatic,PlotRange->All]


and add some fancy replacement/smothing for deleted points:

DateListPlot[
{ts,
TimeSeriesResample[deleteTS[ts,{4}],
ResamplingMethod->{"Interpolation",InterpolationOrder->2}]},
PlotMarkers->Automatic,PlotRange->All]