tspy.time_series module

main entry-point for creation of TimeSeries

tspy.time_series.time_series(*args, **kwargs)

creates a single-time-series object using data in either

  1. list

  2. dataframe

  3. time-series-reader

  4. observation collection

Parameters
data:

type list or pandas.DataFrame or TimeSeriesReader or ObservationCollection

ts_funcfunc, optional=None

(only use with data is a list) if used, it is the function to combine duplicate time-ticks (default is do not combine)

granularitydatetime.timedelta, optional

the granularity for use in time-series TRS (default is None if no start_time, otherwise 1ms)

start_timedatetime, optional

the starting date-time of the time-series (default is None if no granularity, otherwise 1970-01-01 UTC)

ts_columnstring, optional

(only use with data is a pd.DataFrame) the name of the column containing timestamps used in retrieving timestamps (default is using timestamps based on record index)

value_columnstring or list, optional

(only use with data is a pd.DataFrame) the name of the column containing values used in retrieving values (default is create value using all columns)

Returns
TimeSeries

a new time-series

Examples

  • create a simple pandas dataframe

>>> import numpy as np
>>> import pandas as pd
>>> data = np.array([['', 'key', 'timestamp', "value"],                     ['', "a", 1, 27],                     ['', "b", 3, 4],                     ['', "a", 5, 17],                     ['', "a", 3, 7],                     ['', "b", 2, 45]                    ])
>>> df = pd.DataFrame(data=data[1:, 1:],                      index=data[1:, 0],                      columns=data[0, 1:]).astype(dtype={'key': 'object', 'timestamp': 'int64', 'value': 'float64'})
>>> df
key  timestamp  value
  a          1   27.0
  b          3    4.0
  a          5   17.0
  a          3    7.0
  b          2   45.0

create a time-series from a dataframe specifying a timestamp and value column

>>> ts = tspy.time_series(df, ts_column="timestamp", value_column="value")
>>> ts
TimeStamp: 1     Value: 27.0
TimeStamp: 2     Value: 45.0
TimeStamp: 3     Value: 4.0
TimeStamp: 3     Value: 7.0
TimeStamp: 5     Value: 17.0

create a time-series from a dataframe specifying only a timestamp column - it will uses all other columns and stores as value as a single dictionary.

>>> ts = tspy.time_series(df, ts_column="timestamp")
>>> ts
TimeStamp: 1     Value: {value=27.0, key=a}
TimeStamp: 2     Value: {value=45.0, key=b}
TimeStamp: 3     Value: {value=4.0, key=b}
TimeStamp: 3     Value: {value=7.0, key=a}
TimeStamp: 5     Value: {value=17.0, key=a}

create a time-series from a dataframe specifying no timestamp or value column

>>> ts = tspy.time_series(df)
>>> ts
TimeStamp: 0     Value: {value=27.0, key=a, timestamp=1}
TimeStamp: 1     Value: {value=4.0, key=b, timestamp=3}
TimeStamp: 2     Value: {value=17.0, key=a, timestamp=5}
TimeStamp: 3     Value: {value=7.0, key=a, timestamp=3}
TimeStamp: 4     Value: {value=45.0, key=b, timestamp=2}

create a time-series from a dataframe specifying a timestamp column and using a time-reference-system

>>> import datetime
>>> start_time = datetime.datetime(1990, 7, 6)
>>> granularity = datetime.timedelta(weeks=1)
>>> ts = tspy.time_series(df, ts_column="timestamp", granularity=granularity, start_time=start_time)
>>> ts
TimeStamp: 1990-07-13T00:00Z     Value: {value=27.0, key=a}
TimeStamp: 1990-07-20T00:00Z     Value: {value=45.0, key=b}
TimeStamp: 1990-07-27T00:00Z     Value: {value=4.0, key=b}
TimeStamp: 1990-07-27T00:00Z     Value: {value=7.0, key=a}
TimeStamp: 1990-08-10T00:00Z     Value: {value=17.0, key=a}
  • create a time-series from a list of values

>>> ts = tspy.time_series([0, 1])
>>> ts
TimeStamp: 0     Value: 0
TimeStamp: 1     Value: 1

create a time-series from a list of values with a time-reference system

>>> import datetime
>>> granularity = datetime.timedelta(days=1)
>>> start_time = datetime.datetime(1990,7,6)
>>> ts = tspy.time_series([0, 1], granularity=granularity, start_time=start_time)
>>> ts
TimeStamp: 1990-07-06T00:00Z     Value: 0
TimeStamp: 1990-07-07T00:00Z     Value: 1
  • create a collection of observations

>>> import tspy
>>> observations = tspy.builder().add(tspy.observation(0,0)).add(tspy.observation(1,1)).result()
>>> observations
[(0,0),(1,1)]

create a time-series from observations

>>> ts = tspy.time_series(observations)
>>> ts
TimeStamp: 0     Value: 0
TimeStamp: 1     Value: 1

create a time-series from observations with a time-reference system

>>> import datetime
>>> granularity = datetime.timedelta(days=1)
>>> start_time = datetime.datetime(1990,7,6)
>>> ts = tspy.time_series(observations, granularity=granularity, start_time=start_time)
>>> ts
TimeStamp: 1990-07-06T00:00Z     Value: 0
TimeStamp: 1990-07-07T00:00Z     Value: 1