How to specify RegularlySampledQ TimeSeries with EndOfMonth date unit MinimumTimeIncrement?


"EndOfMonth" is date unit that can be used with date functions. When specifying a TimeSeries object with date increments you can specify the date unit increment so that you obtain a regularly sampled time series. I need date units of "EndOfMonth" in the TimeSeries.

If I supply the month end dates the result is an irregularly sampled time series.

  Transpose@{DateRange[{2006, 12, 31}, {2007, 9, 30}, "EndOfMonth"], 
    Range[10]}] // RegularlySampledQ
(* False *)

If I request a time increment of "EndOfMonth" then TimeSeries errors.

TimeSeries[Range[10], {Automatic, {2007, 9, 30}, {1, "EndOfMonth"}}]

TemporalData::tpspc: The time specification {Automatic,{2007,9,30},{1,EndOfMonth}} should be one of Automatic, a time range, a list containing a list of explicit times, or a list containing one such specification for each path in the data.

If I request a time increment of "Month" I get a regularly sampled time series but the dates are not month ends.

(m = TimeSeries[
    Range[10], {Automatic, {2007, 9, 30}, {1, "Month"}}]) // RegularlySampledQ

(* True *)

(m["Dates"] /. DateObject[d_, ___] :> DateObject[d]) === 
 DateRange[DateObject@{2006, 12, 31}, DateObject@{2007, 9, 30}, "EndOfMonth"]

(* False *)

How can a regularly sampled TimeSeries of date unit "EndOfMonth" be created?


Posted 2016-06-08T12:52:31.550

Reputation: 35 657



This is the purpose of the TemporalRegularity option.

TemporalRegularity is an option for TemporalData, TimeSeries, and EventSeries that controls whether the paths are assumed to be uniformly spaced in time.

When setting this option, the dates themselves are ignored and a standard index {0,1,...,n} is used in its place, allowing for non-uniform dates/times to be used for labeling and plotting purposes while leveraging algorithms that require uniform spacing.

  Transpose@{DateRange[{2006, 12, 31}, {2007, 9, 30}, "EndOfMonth"], 
    Range[10]}, TemporalRegularity -> True] // RegularlySampledQ

(* True *)


It is interesting to look at the InputForm of the TimeSeries object that is produced by setting TemporalRegularity to True. Note the use of a uniform time specification.

TemporalData[TimeSeries, {{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}, 
  {TemporalData`UniformTimeSpecification[{3376512000, 3379190400, 3381609600, 
    3384288000, 3386880000, 3389558400, 3392150400, 3394828800, 3397507200, 
    3400099200}]}, 1, {"Continuous", 1}, {"Discrete", 1}, 1, 
  {ValueDimensions -> 1, DateFunction -> Automatic, 
   ResamplingMethod -> {"Interpolation", InterpolationOrder -> 1}, 
   TemporalRegularity -> True}}, True, 10.1]

Obviously this is exposing internal functionality that is subject to change.

Andy Ross

Posted 2016-06-08T12:52:31.550

Reputation: 18 640

The solution was staring at me the whole time. LOL! (+1) I don't think there will be a better answer but we'll let it sit nonetheless. – Edmund – 2016-06-14T09:21:13.020

@Edmund happens to me all the time :) – Andy Ross – 2016-06-14T12:20:00.507

1@AndyRoss, given an aribtrary DateObject with month and possibly date specified, how to return EndOfMonth – alancalvitti – 2018-07-23T03:01:04.840