# Inconsistent arithmetic of TimeSeries with non-equally spaced data?

Consider the following non-equally spaced TimeSeries:

ts = TimeSeries[
Prepend[ConstantArray[1, 99],
0], {Prepend[(100 + Range)/100, 0]}];
ListLinePlot[ts, PlotMarkers -> {Automatic, 12}] Visually, geometry suggests the Mean ought to be roughly 3/4. However,

Mean[ts]
(*99/100*)


This is the same as Mean[ts["Values"]]. In other words, Mean is computed using the underlying values, assuming they're equally spaced... even if they're not.

In contrast, Mean@TimeSeriesResample@ts returns $149/200\approx 3/4$. By resampling with equal spacing, which roughly corresponds to applying Mean to the underlying continuous interpolating function, we get the expected result.

As a result, my initial hypothesis of how functions are applied to TimeSeries was f_[t_TimeSeries]:=f_[t["Values"]].

Now consider Differences, which provides a counterexample:

Differences[ts]["Values"][[1 ;; 3]]
(*{1/101, 1/101, 0}*)

Differences[ts["Values"]][[1 ;; 3]]
(*{1, 0, 0}*)


Unlike Mean, it appears Differences of a TimeSeries somehow takes into account the spacing of the datapoints.

So my question is:

• Is there a consistent rule for how functions (such as Mean or Differences) are computed for TimeSeries with unequally-spaced data?

Personally, my intuition is that applying a function to a TimeSeries ought to roughly correspond to applying that function to the underlying InterpolatingFunction of the data. However, that's debatable, and Mean clearly wasn't implemented that way.

This has caused some unexpected results while I was working with unequally-spaced TimeSeries.

## 2 Answers

The Mean[] you're looking for is

1/2 NIntegrate[ts[t], {t, 0, 2}]
(* 0.747499 *)


This is not the rule you are looking for but: - if the function to be applied does not care about seriality of the data but operates on a collection, like Mean, then it will be applied to values - if the function cares in which order the data comes and in what steps, like Differences and Accumulate, then it will resample the data to ensure consistent step.