[Python] How can I iterate over all columns using pl.all() in Polars?

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I've written a custom function in Polars to generate a horizontal forward/backward fill list of expressions. The function accepts an iterable of expressions (or column names) to determine the order of filling. I want to to use all columns via pl.all() as default. The problem is that pl.all() returns a single expression rather than an iterable, so trying to reverse or iterate over it leads to a TypeError.

    Is there a way to convert between single expressions and iterables of expressions? Any suggestions or workarounds are greatly appreciated!

    Here is the function:

    from typing import Iterable
    from polars._typing import IntoExpr
    import polars as pl

    def fill_horizontal(exprs: Iterable[IntoExpr], forward: bool = True) -> list[pl.Expr]:
    """Generate a horizontal forward/backward fill list of expressions."""
    # exprs = exprs or pl.all() # use all columns as default
    cols = [col for col in reversed(exprs)] if forward else exprs
    return [pl.coalesce(cols[i:]) for i in range(0, len(cols) - 1)]


    Here is an example:

    df = pl.DataFrame({
    "col1": [1, None, 2],
    "col2": [1, 2, None],
    "col3": [None, None, 3]})
    print(df)
    # shape: (3, 3)
    # ┌──────┬──────┬──────┐
    # │ col1 ┆ col2 ┆ col3 │
    # │ --- ┆ --- ┆ --- │
    # │ i64 ┆ i64 ┆ i64 │
    # ╞══════╪══════╪══════╡
    # │ 1 ┆ 1 ┆ null │
    # │ null ┆ 2 ┆ null │
    # │ 2 ┆ null ┆ 3 │
    # └──────┴──────┴──────┘
    print('forward_fill')
    print(df.with_columns(fill_horizontal(df.columns, forward=True)))
    # shape: (3, 3)
    # ┌──────┬──────┬──────┐
    # │ col1 ┆ col2 ┆ col3 │
    # │ --- ┆ --- ┆ --- │
    # │ i64 ┆ i64 ┆ i64 │
    # ╞══════╪══════╪══════╡
    # │ 1 ┆ 1 ┆ 1 │
    # │ null ┆ 2 ┆ 2 │
    # │ 2 ┆ 2 ┆ 3 │
    # └──────┴──────┴──────┘
    print('backward_fill')
    print(df.with_columns(fill_horizontal(df.columns, forward=False)))
    # shape: (3, 3)
    # ┌──────┬──────┬──────┐
    # │ col1 ┆ col2 ┆ col3 │
    # │ --- ┆ --- ┆ --- │
    # │ i64 ┆ i64 ┆ i64 │
    # ╞══════╪══════╪══════╡
    # │ 1 ┆ 1 ┆ null │
    # │ 2 ┆ 2 ┆ null │
    # │ 2 ┆ 3 ┆ 3 │
    # └──────┴──────┴──────┘

    Continue reading...

Compartilhe esta Página