ktk.TimeSeries.add_data#
- TimeSeries.add_data(data_key, data_value, *, overwrite=False, in_place=False)[source]#
Add new data to the TimeSeries.
Although we can directly assign values to the data property:
ts.data["name"] = value
this method provides an alternative way to add data to the TimeSeries:
ts = ts.add_data(name, value, ...)
with the following advantages:
Overwrite prevention: Setting the overwrite argument determines explicitly if you want existing data with the same name to be overwritten or not.
Size check: Additional data is compared to the contents of the TimeSeries to ensure that it has the correct dimensions. See Raises section for more information.
Size matching: Constant “series” such as [3.0], which is a one-sample series of 3.0, are automatically expanded to match the size of the TimeSeries. For example, if the TimeSeries has 4 samples, then the input data is expanded to [3.0, 3.0, 3.0, 3.0].
- Parameters:
data_key (str) – Name of the data key.
data_value (ArrayLike) – Any data that can be converted to a NumPy array
overwrite (bool) – Optional. True to overwrite if there is already a data key of this name in the TimeSeries. Default is False.
in_place (bool) – Optional. True to modify and return the original TimeSeries. False to return a modified copy of the TimeSeries while leaving the original TimeSeries intact. Default is False.
- Returns:
The TimeSeries with the added data.
- Return type:
- Raises:
ValueError – In any of these conditions: If data with this key already exists and overwrite is False. If the size of the data (first dimension) does not match the size of existing data or the existing time. If data is a pandas DataFrame and its index does not match the existing time.
Examples
>>> ts = ktk.TimeSeries() >>> ts = ts.add_data("data1", [1.0, 2.0, 3.0]) >>> ts = ts.add_data("data2", [4.0, 5.0, 6.0]) >>> ts TimeSeries with attributes: time: array([], dtype=float64) data: {'data1': array([1., 2., 3.]), 'data2': array([4., 5., 6.])} time_info: {'Unit': 's'} data_info: {} events: []
# Size matching example >>> ts = ktk.TimeSeries(time = [0.0, 0.1, 0.2, 0.3]) >>> ts = ts.add_data(“data1”, [9.9]) >>> ts TimeSeries with attributes:
time: array([0. , 0.1, 0.2, 0.3]) data: {‘data1’: array([9.9, 9.9, 9.9, 9.9])}
time_info: {‘Unit’: ‘s’} data_info: {}
events: []