Skip to content

mecfs_bio.build_system.task.pipes.cast_to_float32_pipe

Classes:

CastFloatsToFloat32Pipe

Bases: DataProcessingPipe

Convert all float64 columns to float32, except those named in cols_to_exclude.

Methods:

Attributes:

cols_to_exclude class-attribute instance-attribute

cols_to_exclude: Sequence[str] = ()

process

process(x: LazyFrame) -> narwhals.LazyFrame
Source code in mecfs_bio/build_system/task/pipes/cast_to_float32_pipe.py
def process(self, x: narwhals.LazyFrame) -> narwhals.LazyFrame:
    schema = x.collect_schema()
    missing = set(self.cols_to_exclude) - set(schema.names())
    assert not missing, (
        f"Excluded columns not present in frame: {sorted(missing)}.  List of present columns: {sorted(schema.names())}"
    )
    cols_to_cast = [
        col_name
        for col_name, dtype in schema.items()
        if dtype == narwhals.Float64 and col_name not in self.cols_to_exclude
    ]
    return x.with_columns(narwhals.col(cols_to_cast).cast(narwhals.Float32))