ktk.TimeSeries.from_dataframe#
- static TimeSeries.from_dataframe(dataframe, /, *, time_info={'Unit': 's'}, data_info={}, events=[])[source]#
Create a new TimeSeries from a Pandas Dataframe.
Data in column which names end with bracketed indices such as [0], [1], [0,0], [0,1], etc. are converted to multidimensional arrays. For example, if a DataFrame has these column names:
"Forces[0]", "Forces[1]", "Forces[2]", "Forces[3]"
then a single data key is created (“Forces”) and the shape of the data is Nx4.
- Parameters:
dataframe (DataFrame) – A Pandas DataFrame where the index corresponds to time, and where each column corresponds to a data key.
time_info (dict[str, Any]) – Optional. Will be copied to the TimeSeries’ time_info attribute.
data_info (dict[str, dict[str, Any]]) – Optional. Will be copied to the TimeSeries’ data_info attribute.
events (list[TimeSeriesEvent]) – Optional. Will be copied to the TimeSeries’ events attribute.
- Returns:
The converted TimeSeries.
- Return type:
See also
ktk.TimeSeries.to_dataframe
,ktk.TimeSeries.from_array
,ktk.TimeSeries.to_array
Examples
Example with unidimensional data
Create a DataFrame with two series of 3 samples:
>>> import pandas as pd >>> df = pd.DataFrame([[1., 2.], [3., 4.], [5., 6.]]) >>> df.columns = ["test1", "test2"] >>> df test1 test2 0 1.0 2.0 1 3.0 4.0 2 5.0 6.0
Convert to a TimeSeries:
>>> ts = ktk.TimeSeries.from_dataframe(df) >>> ts.data {'test1': array([1., 3., 5.]), 'test2': array([2., 4., 6.])}
Example with multidimensional data
Create a DataFrame with one series of 3 samples of dimension 2:
>>> df = pd.DataFrame([[1., 2.], [3., 4.], [5., 6.]]) >>> df.columns = ["test[0]", "test[1]"] >>> df test[0] test[1] 0 1.0 2.0 1 3.0 4.0 2 5.0 6.0
Convert to a TimeSeries:
>>> ts = ktk.TimeSeries.from_dataframe(df) >>> ts.data {'test': array([[1., 2.], [3., 4.], [5., 6.]])}
Example with even more dimensions
Create a DataFrame with one series of 5 samples of dimension 2x2 (rot) and one series of 5 samples of dimension 2 (trans):
>>> df = pd.DataFrame() >>> df.index = [0., 0.1, 0.2, 0.3, 0.4] # Time in seconds >>> df["rot[0,0]"] = np.cos([0., 0.1, 0.2, 0.3, 0.4]) >>> df["rot[0,1]"] = -np.sin([0., 0.1, 0.2, 0.3, 0.4]) >>> df["rot[1,0]"] = np.sin([0., 0.1, 0.2, 0.3, 0.4]) >>> df["rot[1,1]"] = np.cos([0., 0.1, 0.2, 0.3, 0.4]) >>> df["trans[0]"] = [0., 0.1, 0.2, 0.3, 0.4] >>> df["trans[1]"] = [5., 6., 7., 8., 9.] >>> df rot[0,0] rot[0,1] rot[1,0] rot[1,1] trans[0] trans[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
Convert to a TimeSeries:
>>> ts = ktk.TimeSeries(df) >>> ts.data {'rot': 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]]]), 'trans': array([[0. , 5. ], [0.1, 6. ], [0.2, 7. ], [0.3, 8. ], [0.4, 9. ]])}