Skip to content

cast

Cast(target_type, *, features=None)

Bases: Transform

Cast features to a different datatype.

This transform allows to change the datatype of features in a DataFrame. To cast all features to the same datatype, provide a single type as the target_type argument e.g.

transform = Cast(pl.Float64)

By specifying the features keyword argument, only the selected features will be cast e.g.

transform = Cast(pl.Float64, features=["feature_a"])

Lastly, to cast features to different datatypes, provide a dictionary with feature names as keys and target types as values e.g.

transform = Cast(
    {
        "feature_a": pl.Boolean,
        "feature_b": pl.Float64,
    },
)

Initializes the Cast transform.

Parameters:

Name Type Description Default
target_type PolarsDataType | dict[str, PolarsDataType]

Type to which the features will be cast. If a single type is provided, all features or those provided in the features keyword argument will be cast to that specific type. To cast features to different types, provide a dictionary with feature names as keys and target types as values.

required
features Iterable[str] | None

The features to cast. If None all features will be cast. This is the default behaviour.

None
Source code in src/flowcean/polars/transforms/cast.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(
    self,
    target_type: PolarsDataType | dict[str, PolarsDataType],
    *,
    features: Iterable[str] | None = None,
) -> None:
    """Initializes the Cast transform.

    Args:
        target_type: Type to which the features will be cast.
            If a single type is provided, all features or those provided in
            the `features` keyword argument will be cast to that specific
            type. To cast features to different types, provide a dictionary
            with feature names as keys and target types as values.
        features: The features to cast. If `None` all
            features will be cast. This is the default behaviour.
    """
    self.target_type = target_type
    self.features = features