Function Repository Resource:

TimeSeriesSelect

Source Notebook

Filter values out of a time series object

Contributed by: Bob Sandheinrich

ResourceFunction["TimeSeriesSelect"][tseries,crit]

gives a time series containing only elements of tseries for which crit is true.

ResourceFunction["TimeSeriesSelect"][crit]

represents an operator form that can be applied to an expression.

Details and Options

The function crit is applied to the time and value as crit[ti,xi].

Examples

Basic Examples (3) 

Select only even values in a time series:

In[1]:=
evens = ResourceFunction["TimeSeriesSelect"][
  TimeSeries[{{1, 10}, {2, 8}, {3, 13}, {4, 16}}], EvenQ[#2] &]
Out[1]=

The third element of the path is removed:

In[2]:=
evens["Path"]
Out[2]=

Retrieve financial data:

In[3]:=
ts = Entity["Financial", "NASDAQ:AAPL"][
EntityProperty[
  "Financial", "Price", {"Date" -> Interval[{
DateObject[{2015}], 
DateObject[{2019}]}]}]]
Out[3]=

Select low values:

In[4]:=
low = ResourceFunction["TimeSeriesSelect"][
  ts, #2 < Quantity[160., "USDollars"] &]
Out[4]=

Plot the results:

In[5]:=
DateListPlot[{ts, low}, PlotStyle -> {Dotted, Directive[Dashed, Green]}]
Out[5]=

Get some yearly data:

In[6]:=
gdpuk = CountryData["UK", {"GDP", {1970, 2008}}]
Out[6]=

Include only values from Summer Olympics years:

In[7]:=
ResourceFunction["TimeSeriesSelect"][gdpuk, Mod[DateValue[#1, "Year"], 4] === 0 &]
Out[7]=

Scope (3) 

Get financial time series data:

In[8]:=
appl = Entity["Financial", "NASDAQ:AAPL"][
EntityProperty[
  "Financial", "Close", {"Date" -> Interval[{
DateObject[{2010}], 
DateObject[{2015}]}]}]]
Out[8]=

Select only values from the first quarter of each year with a high value (this is meaningless and should not be used for investment decisions):

In[9]:=
chosen = ResourceFunction["TimeSeriesSelect"][appl, DateValue[#1, "Month"] < 4 && QuantityMagnitude[#2] > 60 &]
Out[9]=

Plot the selected values along with the full data:

In[10]:=
DateListPlot[{appl, chosen}, Joined -> False]
Out[10]=

Applications (3) 

Get COVID-19 time series data:

In[11]:=
timeseries = ResourceFunction["NYTimesCOVID19Data"]["USCountiesTimeSeries"];

Limit each time series to only include the non-zero values and take only counties with 28 days or more of data:

In[12]:=
fromfirstdeath = timeseries[
   Select[(#Deaths["FirstDate"] < Now - Quantity[28, "Days"]) &], {"Deaths" -> ResourceFunction["TimeSeriesSelect"][#2 > 0 &]}][All, "Deaths"]
Out[12]=

Plot the values:

In[13]:=
DateListPlot[
 KeySelect[
  Select[fromfirstdeath, #["PathLength"] > 0 &], ! MissingQ[#] &]]
Out[13]=

Version History

  • 1.0.0 – 02 April 2020

Related Resources

Author Notes

This should be extended properly for EventSeries and TemporalData.

There are issues with QuantityArray based TimeSeries.

License Information