Elements of a TimeSeries can be accessed just like their regular MaskedArray counterparts, using an integer, a sequence of integers, an array of integers or booleans, a slice, or a string corresponding to a field name (if the series has a structured dtype). In addition, a TimeSeries can be indexed by:
In the last two cases, the strings are converted to a Date object with the same frequency as the TimeSeries instance.
If a single element is accessed on a TimeSeries with a standard dtype (without named fields), the result is either (i) a scalar if the element is not masked, or (ii) the constant masked if the corresponding mask element is True.
Accessing a single element of a TimeSeries with a structured data-type returns either (i) a numpy.void object if the element is not masked or (ii) a MaskedArray otherwise.
Accessing several elements at once always returns a TimeSeries object, with the same frequency as the input.
>>> monthly_start = ts.Date('M', '2005-01')
>>> series = ts.time_series(np.arange(12),
... start_date=monthly_start)
>>> series[0]
0
>>> series[monthly_start+6]
6
>>> series['2005-06-15']
5
>>> series[[0, -1]]
timeseries([ 0 11],
dates = [Jan-2005 Dec-2005],
freq = M)
>>> series[-3:]
timeseries([ 9 10 11],
dates = [Oct-2005 ... Dec-2005],
freq = M)
>>> condition = (series < 7)
>>> series[condition]
timeseries([0 1 2 3 4 5 6],
dates = [Jan-2005 ... Jul-2005],
freq = M)
Two functions provide a convenient way to access the first and last valid (not masked) elements of a TimeSeries:
first_unmasked_val | |
last_unmasked_val |
Individual or multiple elements of a TimeSeries can be set the same way as for MaskedArray.
To mask one or several elements of a TimeSeries, they must be set to the masked constant.
>>> series
timeseries([ 0 1 2 3 4 5 6 7 8 9 10 11],
dates = [Jan-2005 ... Dec-2007],
freq = M)
>>> series[[0, -1]] = ma.masked
>>> series
timeseries([-- 1 2 3 4 5 6 7 8 9 10 --],
dates = [Jan-2005 ... Dec-2005],
freq = M)
>>> series[series.quarter == 2] = 0
>>> series
timeseries([-- 1 2 0 0 0 6 7 8 9 10 --],
dates = [Jan-2005 ... Dec-2005],
freq = M)