ktk.TimeSeries#
- class TimeSeries(src=None, *, time=[], data={}, events=[], info={'Time': {'Unit': 's'}}, **kwargs)[source]#
Bases:
objectA class that holds time, data series, events and metadata.
- Parameters:
src (None | TimeSeries | pd.DataFrame | ArrayLike)
time (ArrayLike)
data (dict[str, ArrayLike])
events (list[TimeSeriesEvent])
info (dict[str, Any])
- time#
Time attribute as 1-dimension np.array.
- Type:
np.ndarray
- data#
Contains the data, where each element contains a np.array which first dimension corresponds to time.
- Type:
dict[str, np.ndarray]
- events#
List of events.
- Type:
list[TimeSeriesEvent]
- info#
Contains metadata such as units or other information.
- Type:
dict[str, Any]
Examples
A TimeSeries can be constructed from another TimeSeries, a Pandas DataFrame or any array with at least one dimension.
Creating an empty TimeSeries:
>>> ktk.TimeSeries() TimeSeries with attributes: time: array([], dtype=float64) data: {} events: [] info: {'Time': {'Unit': 's'}}
Creating a TimeSeries and setting time and data:
>>> ktk.TimeSeries(time=np.arange(0, 10), data={"test":np.arange(0, 10)}) TimeSeries with attributes: time: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) data: {'test': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])} events: [] info: {'Time': {'Unit': 's'}}
Creating a TimeSeries as a copy of another TimeSeries:
>>> ts1 = ktk.TimeSeries(time=np.arange(0, 10), data={"test":np.arange(0, 10)}) >>> ts2 = ktk.TimeSeries(ts1) >>> ts2 TimeSeries with attributes: time: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) data: {'test': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])} events: [] info: {'Time': {'Unit': 's'}}
See Also: TimeSeries.copy
Creating a TimeSeries from a Pandas DataFrame:
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["x"] = [0., 1., 2., 3., 4.] >>> df["y"] = [5., 6., 7., 8., 9.] >>> df["z"] = [0., 0., 0., 0., 0.] >>> df x y z 0.0 0.0 5.0 0.0 0.1 1.0 6.0 0.0 0.2 2.0 7.0 0.0 0.3 3.0 8.0 0.0 0.4 4.0 9.0 0.0
>>> ts = ktk.TimeSeries(df) >>> ts TimeSeries with attributes: time: array([0. , 0.1, 0.2, 0.3, 0.4]) data: <dict with 3 entries> events: [] info: {'Time': {'Unit': 's'}}
>>> ts.data {'x': array([0., 1., 2., 3., 4.]), 'y': array([5., 6., 7., 8., 9.]), 'z': array([0., 0., 0., 0., 0.])}
See Also: TimeSeries.from_dataframe
5. Creating a multidimensional TimeSeries from a Pandas DataFrame (using brackets in column names):
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["point[:,0]"] = [0., 1., 2., 3., 4.] >>> df["point[:,1]"] = [5., 6., 7., 8., 9.] >>> df["point[:,2]"] = [0., 0., 0., 0., 0.] >>> df point[:,0] point[:,1] point[:,2] 0.0 0.0 5.0 0.0 0.1 1.0 6.0 0.0 0.2 2.0 7.0 0.0 0.3 3.0 8.0 0.0 0.4 4.0 9.0 0.0
>>> ts = ktk.TimeSeries(df) >>> ts.data {'point': array([[0., 5., 0.], [1., 6., 0.], [2., 7., 0.], [3., 8., 0.], [4., 9., 0.]])}
See Also: TimeSeries.from_dataframe
6. Creating a multidimensional TimeSeries of higher order from a Pandas DataFrame (using brackets and commas in column names):
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["R[:,0,0]"] = np.cos([0., 0.1, 0.2, 0.3, 0.4]) >>> df["R[:,0,1]"] = -np.sin([0., 0.1, 0.2, 0.3, 0.4]) >>> df["R[:,1,0]"] = np.sin([0., 0.1, 0.2, 0.3, 0.4]) >>> df["R[:,1,1]"] = np.cos([0., 0.1, 0.2, 0.3, 0.4]) >>> df["t[:,0]"] = [0., 0.1, 0.2, 0.3, 0.4] >>> df["t[:,1]"] = [5., 6., 7., 8., 9.] >>> df R[:,0,0] R[:,0,1] R[:,1,0] R[:,1,1] t[:,0] t[:,1] 0.0 1.000000 -0.000000 0.000000 1.000000 0.0 5.0 0.1 0.995004 -0.099833 0.099833 0.995004 0.1 6.0 0.2 0.980067 -0.198669 0.198669 0.980067 0.2 7.0 0.3 0.955336 -0.295520 0.295520 0.955336 0.3 8.0 0.4 0.921061 -0.389418 0.389418 0.921061 0.4 9.0
>>> ts = ktk.TimeSeries(df) >>> ts.data {'R': array([[[ 1. , -0. ], [ 0. , 1. ]], [[ 0.99500417, -0.09983342], [ 0.09983342, 0.99500417]], [[ 0.98006658, -0.19866933], [ 0.19866933, 0.98006658]], [[ 0.95533649, -0.29552021], [ 0.29552021, 0.95533649]], [[ 0.92106099, -0.38941834], [ 0.38941834, 0.92106099]]]), 't': array([[0. , 5. ], [0.1, 6. ], [0.2, 7. ], [0.3, 8. ], [0.4, 9. ]])}
See Also: TimeSeries.from_dataframe
7. Creating a TimeSeries from any array (results in a TimeSeries with a single data key named “data” and with a matching time property with a period of 1 second - unless time attribute is also defined):
>>> ktk.TimeSeries([0.1, 0.2, 0.3, 0.4, 0.5]) TimeSeries with attributes: time: array([0., 1., 2., 3., 4.]) data: {'data': array([0.1, 0.2, 0.3, 0.4, 0.5])} events: [] info: {'Time': {'Unit': 's'}}
>>> ktk.TimeSeries([0.1, 0.2, 0.3, 0.4, 0.5], time=[0.1, 0.2, 0.3, 0.4, 0.5]) TimeSeries with attributes: time: array([0.1, 0.2, 0.3, 0.4, 0.5]) data: {'data': array([0.1, 0.2, 0.3, 0.4, 0.5])} events: [] info: {'Time': {'Unit': 's'}}
See Also: TimeSeries.from_array
Methods
Add new data to the TimeSeries.
Add metadata to TimeSeries' data.
Add an event to the TimeSeries.
Add new info the to TimeSeries.
Deep copy of a TimeSeries.
Count the number of occurrence of a given event name.
Fill missing samples using a given method.
Create a new TimeSeries from an array.
Create a new TimeSeries from a Pandas Dataframe.
Get the time index that is just after the specified event occurrence.
Get the time index that is just after the specified time.
Get the time index that is closest to the specified event occurrence.
Get the time index that is closest to the specified time.
Get the time index that is just before the specified event occurrence.
Get the time index that is just before the specified time.
Get the sample rate in samples/s.
Return a subset of the TimeSeries.
Get a TimeSeries after the specified event.
Get a TimeSeries after the specified time index.
Get a TimeSeries after the specified time.
Get a TimeSeries before the specified event.
Get a TimeSeries before the specified time index.
Get a TimeSeries before the specified time.
Get a TimeSeries between two specified events.
Get a TimeSeries between two specified time indexes.
Get a TimeSeries between two specified times.
Return a boolean array of missing samples.
Merge the TimeSeries with another TimeSeries.
Plot the TimeSeries in the current matplotlib figure.
Remove a key in data.
Remove metadata from a TimeSeries' data.
Remove events with same name and time so that each event gets unique.
Remove an event occurrence or all events of a same name.
Remove info from a TimeSeries.
Rename a key in data.
Rename an event occurrence or all events of a same name.
Rename info keys.
Resample the TimeSeries.
Shift time and events.time.
Deprecated.
Create a DataFrame by reshaping all data to one bidimensional table.
Delete the events that are outside the TimeSeries' time attribute.
Edit events interactively.
Synchronize one or two TimeSeries by shifting their time.
Attributes