Skip to main content
Version: 0.0.98

ts_shape.features.stats.feature_table

Classes
NameDescription
DescriptiveFeaturesCompute descriptive statistics for a DataFrame, grouped by UUID.

DescriptiveFeatures​

classsource
DescriptiveFeatures(dataframe: pd.DataFrame)

Compute descriptive statistics for a DataFrame, grouped by UUID.

Use compute to get descriptive statistics for each UUID in the DataFrame.

Attributes
NameTypeDescription
dataDataFrameDataFrame containing the data.
Functions
NameDescription
overall_statsCompute and return overall statistics for the DataFrame group.
compute_per_groupCompute and return statistics for each column in the DataFrame group.
computeCompute and return descriptive statistics for each UUID in the DataFrame.
compute_windowedDescriptive statistics per time window, all signals in one row.
Parameters
NameTypeDescriptionDefault
dataframeDataFrameDataFrame containing the data.required

overall_stats​

methodsource
overall_stats(group: pd.DataFrame) -> dict[str, int | float]

Compute and return overall statistics for the DataFrame group.

The returned dictionary contains:

  • total_rows: Total number of rows in the group.
  • total_time: Total time difference from max and min of 'systime' column.
  • is_delta_sum: Sum of the 'is_delta' column.
  • is_delta_avg: Mean of the 'is_delta' column.
  • is_delta_std: Standard deviation of the 'is_delta' column.

The is_delta_* values are NaN when the group has no is_delta column (or no value in it).

Parameters
NameTypeDescriptionDefault
groupDataFrameDataFrame group (rows of a single UUID) to summarize.required
Returns
NameTypeDescription
dictdict[str, int | float]A dictionary with overall statistics.

compute_per_group​

methodsource
compute_per_group(group: pd.DataFrame) -> dict[str, dict[str, Any]]

Compute and return statistics for each column in the DataFrame group.

Parameters
NameTypeDescriptionDefault
groupDataFrameDataFrame group (rows of a single UUID) to summarize.required
Returns
NameTypeDescription
dictdict[str, dict[str, Any]]A dictionary with overall statistics, and string, numeric, and boolean statistics per column. The long-format value columns are classified by name (value_bool boolean, value_double / value_integer numeric, value_string string), so every signal has the same keys; other columns by dtype. systime, uuid and is_delta are not described per column.

compute​

methodsource
compute(
output_format: str = "dict",
*,
window: str | pd.Timedelta | pd.DateOffset | None = None
) -> pd.DataFrame | dict[str, dict[str, dict[str, Any]]]

Compute and return descriptive statistics for each UUID in the DataFrame.

Parameters
NameTypeDescriptionDefault
output_formatstrThe desired output format: 'dict', 'dataframe' or 'windowed' (one row per time window, see compute_windowed). Defaults to 'dict'.'dict'
windowstr | Timedelta | DateOffset | NoneWindow size for output_format="windowed" (default "1s"); not allowed with the other formats.None
Returns
TypeDescription
DataFrame | dict[str, dict[str, dict[str, Any]]]Union[DataFrame, dict]: A DataFrame or a nested dictionary with the UUID as the key and specific statistics related to that UUID's data type. An empty frame gives {} (or an empty DataFrame). With output_format="windowed" the frame of compute_windowed.
Raises
TypeDescription
ValueErrorFor an unknown output_format, or a window with a format other than 'windowed'.
ColumnNotFoundErrorIf the frame has no uuid column.

compute_windowed​

methodsource
compute_windowed(
window: str | pd.Timedelta | pd.DateOffset = "1s",
*,
metrics: list[str] | None = None,
column_separator: str = "__",
time_column: str = "systime"
) -> pd.DataFrame

Descriptive statistics per time window, all signals in one row.

One row per window of window on a regular grid, from the window of the first timestamp to the window of the last one: every window is present, also one without any data. The windows are the bins of pd.Grouper(freq=window), wall-clock windows in the data's time zone (a "1D" window on a DST day lasts 23 or 25 hours).

Each signal gets the statistics compute reports for it, under the same names: the overall ones (total_rows, total_time, is_delta_sum, is_delta_avg, is_delta_std, plus total_time_seconds) and those of its value kind, read from the long-format value column the signal fills (value_double / value_integer numeric, value_bool boolean, value_string string; the most filled one when it fills several). A cell holds what compute gives for that signal on the rows of that window.

A window where a signal has no row gives NaN, except for the counts (total_rows, true_count, false_count, unique_values, count_most_frequent, count_null, contains_digit_count, frequency_least_common), which are 0 (int64). The boolean mode and is_balanced are 1.0 / 0.0, total_time is a Timedelta, the string values (most_frequent, longest_string, ...) are text; every other column is float64.

All statistics are grouped aggregations over (signal, window), with no loop over windows.

Parameters
NameTypeDescriptionDefault
windowstr | Timedelta | DateOffsetWindow size, a pandas offset ("1s", "15min", "1h", "1D", "MS") or a Timedelta.'1s'
metricslist[str] | NoneFeature names to compute; None computes all. A name that does not apply to a signal's kind is skipped for that signal.None
column_separatorstrSeparator between uuid and feature in the column names.'__'
time_columnstrTimestamp column. Rows without a timestamp are left out.'systime'
Returns
TypeDescription
DataFrameDataFrame with start, end, duration_seconds and one column {uuid}{column_separator}{feature} per signal and feature, signals sorted by uuid, features in the order of compute. start / end are tz-aware when the input is. Only the window columns when the frame has no timestamp.
Raises
TypeDescription
ValueErrorFor a name in metrics that is no feature.
ColumnNotFoundErrorIf the frame has no uuid or time_column column.
Example
import pandas as pd
from ts_shape.features.stats.feature_table import DescriptiveFeatures

df = pd.DataFrame({
"systime": pd.to_datetime(
["2024-01-01 00:00:00.2", "2024-01-01 00:00:00.7",
"2024-01-01 00:00:02.5"]
),
"uuid": "temp",
"value_double": [20.0, 22.0, 21.0],
})
out = DescriptiveFeatures(df).compute_windowed(
"1s", metrics=["total_rows", "mean"]
)
print(out[["start", "temp__total_rows", "temp__mean"]])