5

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"}]?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
George Wolfe
  • 5,462
  • 21
  • 43

1 Answers1

3

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"]

TimeSeries result which shows FIVE data points, i.e. one less than the original the dates remaining in the time series, showing that Jan 2nd was removed

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.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
  • Wow! How did you know about that function. I propose that a Kevin Mitnick Badge be created for this kind of thing, and for you to be the first to be awarded it. – George Wolfe Mar 19 '21 at 02:21
  • Of course, the Mitnick would have to be retroactively awarded to giants on whose shoulders you have stood. – George Wolfe Mar 19 '21 at 02:27
  • @George Glad you like it! I've added a narrative of how the search unfolded to my answer. I was glad of your question: it was a fun chance to meander behind the scenes for a bit. – MarcoB Mar 19 '21 at 03:28
  • I've been trying to figure out what "Part" did. I didn't get as far as the selection of the path. "Part" is in the documentation, as [[ ]], but it's misleading in this context. So, this function doesn't really drop anything; it extracts the complement of the unwanted dates. – George Wolfe Mar 19 '21 at 03:45
  • Regarding the internal functions I would like to add disclaimer "use at your own risk". If the date is at the end of time range, the observation can be eliminated using TimeSeriesWindow. TimeSeriesDrop is a valid suggestion though, noted. – Gosia Mar 30 '21 at 20:49