Skip to content

[Class] Timeseries

João Saraiva edited this page Jul 22, 2022 · 7 revisions

A Timeseries is an ordered sequence of samples with some associated properties. A Timeseries can be contiguous or discontiguous in time, i.e., all samples might have been acquired with no interruptions or not. If they were acquired discontiguously, then the samples acquired in each of those time intervals are called Segments.

Look at the example below with three discontiguous Segments, where there were interruptions in the acquisition:

contiguous_discontiguous_timeseries

How to instantiate a Timeseries

Give the array(s) of samples, and the properties enumerated below:

  • Sampling Frequency (in Hertz, float)
  • Units (in Unit) [optional]
  • Name (in str) [optional]

Example 1: Instantiate a contiguous Timeseries

ts = Timeseries([1., 2., 3., ...], initial_datetime=datetime(2022, 6, 6, 19, 0), sampling_frequency=64.0, units=Volt())

Example 2: Instantiate a dicontiguous Timeseries

ts = Timeseries.withDiscontiguousSegments({date1: [1., 2., 3., ...], date2: [7., 8., 9., ...], }, sampling_frequency=64.0, units=Volt())

You don't need to worry about what a Segment is, as long as you declare interruptions in acquisitions by giving the arrays keyed with their correct initial datetime.

Getters and Setters

You can get the following properties:

  • initial_datetime returns the timestamp of the first sample (in datetime)
  • final_datetime returns the timestamp of the last sample (in datetime)
  • sampling_frequency returns the sampling frequency in Hertz (in float)
  • units returns the units in which samples should be interperted (in Unit)
  • name returns the name of the Timeseries, if it was defined (in str)

You can also set name after instantiation.

To get the number of samples defined in all the domain of the Timeseries, use len. E.g.: x = len(ts)

Indexing

One Sample

x = ts['2022-01-01 12:00'] returns the sample acquired on the first of January 2022 at noon.

x = ts[datetime(2022, 1, 1, 12)] returns the same.

Raises Index Error when the given timestamp is not defined, i.e., no sample was acquired at that timestamp.

Multiple Samples

x = ts['2022-01-01 12:00', '2022-01-02 12:00', ...] returns the sample acquired on the first and second of January 2022 at noon, in a tuple.

x = ts[datetime(2022, 1, 1, 12), datetime(2022, 1, 2, 12), ...] returns the same.

Raises Index Error if a given timestamp is not defined, i.e., no sample was acquired at that timestamp.

Slices

ts2 = ts1['2022-01-01 12:00' : '2022-01-01 13:30'] gets the slice of ts1 from 12 AM to 1:30 PM, and stores it in ts2.

ts2 = ts1[datetime(2022, 1, 1, 12) : datetime(2022, 1, 2, 13, 30)] returns the same.

Raises Index Error when the interval given is out of boundaries.

Operations

Concatenation

ts3 = ts1 + ts2 concatenates the beginning of ts2 to the end of ts1, and stores it in ts3.

ts1 += ts2 concatenates the beginning of ts2 to the end of ts1, and stores it in ts1.

Raises Arithemetic Error when the Timeseries do not have the same sampling frequency or units; or when the ts1 comes before ts2 (in time).