Masking Values in Time Series#

Masking values is done in the same way as for Table objects (see Masking and Missing Values). The most convenient way to use masking is to initialize a TimeSeries object using the masked=True option.

Example#

We start by initializing a TimeSeries object with masked=True:

>>> from astropy import units as u
>>> from astropy.timeseries import TimeSeries
>>> ts = TimeSeries(time_start='2016-03-22T12:30:31',
...                 time_delta=3 * u.s,
...                 n_samples=5, masked=True)

We can now add some data to our time series:

>>> ts['flux'] = [1., -2., 5., -1., 4.]

As you can see, some of the values are negative. We can mask these using:

>>> ts['flux'].mask = ts['flux'] < 0
>>> ts
<TimeSeries masked=True length=5>
          time            flux
          Time          float64
----------------------- -------
2016-03-22T12:30:31.000     1.0
2016-03-22T12:30:34.000      --
2016-03-22T12:30:37.000     5.0
2016-03-22T12:30:40.000      --
2016-03-22T12:30:43.000     4.0

We can also access the mask values:

>>> ts['flux'].mask
array([False,  True, False,  True, False]...)

Masks are column-based, so masking a single cell does not mask the whole row. Having masked cells then allows functions that normally understand masked values and operate on columns to ignore the masked entries:

>>> import numpy as np
>>> np.min(ts['flux'])
1.0
>>> np.ma.median(ts['flux'])
4.0