Dates manipulation

TimeSeries.adjust_endpoints(a[, start_date, ...]) Returns a TimeSeries going from start_date to end_date.
TimeSeries.compressed(series) Suppresses missing values from a time series.
TimeSeries.fill_missing_dates(data[, dates, ...]) Finds and fills the missing dates in a time series.

Shifts and alignments

Aligning series

adjust_endpoints(a[, start_date, end_date, copy]) Returns a TimeSeries going from start_date to end_date.
align_series(*series, **kwargs) Aligns several TimeSeries, so that their starting and ending dates match.
aligned(*series, **kwargs) Aligns several TimeSeries, so that their starting and ending dates match.

Shifting series

Rates of change or differences in a TimeSeries can be calculated easily using the tshift method.

As an illustration, let us suppose we need to calculate a term-to-term return rate for a monthly series mser. We could initially try something along the lines of

>>> mser = ts.time_series(np.arange(1., 13.),
...                       start_date=ts.Date('M','2001-01'))
>>> tot_change = 100 * (mser[3:]/mser[:-3] - 1)
>>> tot_change
masked_array(data = [ 300.          150.          100.
                       75.           60.           50.
                       42.85714286   37.5          33.33333333],
             mask = False,
       fill_value = 1e+20)

This gives the correct numerical result, but as mser[3:] and mser[:-3] have different starting and ending dates, the result is forced to a plain numpy.ma.MaskedArray. Moreover, yoy_change does not have the same size as the original input series, which may be inconvenient.

A solution consists in using the tshift method.

>>> yoy_change = 100 * (mser/mser.tshift(-3, copy=False) - 1)

The command mser.tshift(-3, copy=False) returns a series with the same dates as mser, but with values shifted 3 periods to the right. The first 3 values of the resulting series are masked.

Note

By default, the tshift copies the original series. For simpler cases as the one above, a copy may be avoided by using the copy=False.

TimeSeries.tshift(series, nper[, copy]) Returns a series of the same size as series, with the same start_date and end_date, but values shifted by nper.