Skip to content

is_time_series

is_timeseries_feature(target, name)

Check if the given column is a time series feature.

A time series feature contains a list of structs with fields time and value.

Parameters:

Name Type Description Default
target DataFrame | LazyFrame | Schema

The LazyFrame, DataFrame or schema to check.

required
name str

The column to check.

required

Returns:

Type Description
bool

True if the column is a time series feature, False otherwise.

Source code in src/flowcean/polars/is_time_series.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def is_timeseries_feature(
    target: pl.DataFrame | pl.LazyFrame | pl.Schema,
    name: str,
) -> bool:
    """Check if the given column is a time series feature.

    A time series feature contains a list of structs with fields _time_ and
    _value_.

    Args:
        target: The LazyFrame, DataFrame or schema to check.
        name: The column to check.

    Returns:
        True if the column is a time series feature, False otherwise.
    """
    if isinstance(target, pl.Schema):
        data_type = target.get(name)
    elif isinstance(target, pl.DataFrame):
        data_type = target.schema.get(name)
    elif isinstance(target, pl.LazyFrame):
        data_type = target.select(name).collect_schema().dtypes()[0]

    if data_type is None or data_type.base_type() != pl.List:
        return False

    inner_type: pl.DataType = cast(pl.DataType, cast(pl.List, data_type).inner)
    if inner_type.base_type() != pl.Struct:
        return False

    field_names = [field.name for field in cast(pl.Struct, inner_type).fields]
    return "time" in field_names and "value" in field_names