Skip to content

mecfs_bio.build_system.task.pipes.format_numbers_pipe

Pipe to format a column of floating point values

Classes:

FormatFloatNumbersPipe

Bases: DataProcessingPipe

Pipe to set the format for floating point values in a given column.

Methods:

Attributes:

col instance-attribute

col: str

format_str instance-attribute

format_str: str

process

process(x: LazyFrame) -> narwhals.LazyFrame
Source code in mecfs_bio/build_system/task/pipes/format_numbers_pipe.py
def process(self, x: narwhals.LazyFrame) -> narwhals.LazyFrame:
    collected = x.collect().to_pandas()
    result_col = []
    for value in collected[self.col].tolist():
        if _convertible_to_float(value):
            result_col.append(f"{float(value):{self.format_str}}")
        else:
            result_col.append(value)
    collected[self.col] = result_col
    return narwhals.from_native(collected).lazy()