From the documentation, TimeSeriesInsert[ts, {"January 1st, 2013", 900}] is clean and easy. How to I make a TimeSeriesDrop function, e.g. TimeSeriesDrop[ts, {"January 1st, 2013"}]?
- 67,153
- 18
- 91
- 189
- 5,462
- 21
- 43
1 Answers
Looking at code from helper functions in the RandomProcesses`TemporalDataUtilitiesDump` context which are implemented in top-level code, and particularly looking at the implementation of the promisingly-named RandomProcesses`TemporalDataUtilitiesDump`iTDDropTimes, I learned that TimeSeries objects have "Take", "Drop", and "Part" properties (or "methods" perhaps?), even though those are not documented (at least as far as I know).
Let's first have a time series to play with:
ts = TimeSeries@ Transpose@ {DateRange[{2021, 1, 1}, {2021, 1, 6}], Range[6]};
Then, inspired by the implementation of RandomProcesses`TemporalDataUtilitiesDump`iTDDropTimes, and conforming to your syntax preference, here is my quick idea:
ClearAll[timeSeriesDrop]
timeSeriesDrop[ts_TemporalData, {drop_}] :=
ts["Part", 1, {Complement[ts["Times"], {AbsoluteTime@DateObject@drop}]}]
In the code above, the $1$ argument after "Part" selects which path in a TemporalData object the operation applies to. Since TimeSeries is effectively a single-path TemporalData object, that is set to 1 here to refer to the first (and only) path in a TimeSeries.
We can use the function as follows:
timeSeriesDrop[ts, {"January 2, 2021"}]
timeSeriesDrop[ts, {"January 2, 2021"}]["DatePath"]
As you can see from the date path above, the January 2nd date was removed from the time series ts, as requested.
This is a rather incomplete implementation; one would want to 1. expand this to handle DateObject input; 2. perhaps most pressing, expand this to handle lists of drop dates. Alternatively, it should also be possible to coax RandomProcesses`TemporalDataUtilitiesDump`iTDDropTimes into service after some modifications.
For the record, here's how my search unfolded. ClearAttributes[TimeSeriesInsert, ReadProtected]; ??TimeSeriesInsert revealed the existence of TemporalData`Utilities`TDDropTimes . Doing the same to the latter function revealed its implementation function, RandomProcesses`TemporalDataUtilitiesDump`iTDDropTimes , which, luckily, is implemented in readable top-level code. It's in that code that I found the "Part", "Take", "Drop" properties. After that, it was a question of some trial and error to figure out how to use the "Part" method.
- 67,153
- 18
- 91
- 189


TimeSeriesWindow.TimeSeriesDropis a valid suggestion though, noted. – Gosia Mar 30 '21 at 20:49