[Python] Polars group_by aggregating by sum is returning an error when using a dictionary

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to do a aggregation from a polars DataFrame. But I'm not getting what I'm expecting.

    This is a minimal replication of the issue:

    import polars as pl

    # Create a DataFrame
    df = pl.DataFrame({"category": ["A", "A", "B", "B", "B"],
    "value": [1., 2., 3., 4., 5.]})

    # Group by 'category' and sum 'value'
    result = df.group_by("category").agg({"value": pl.sum})

    # Print the result
    print(result)



    And I'm getting:

    TypeError: specifying aggregations as a dictionary is not supported


    but I'd like to get:

    ┌──────────┬─────────────────┐
    │ category ┆ value │
    │ --- ┆ --- │
    │ str ┆ list[f64] │
    ╞══════════╪═════════════════╡
    │ A ┆ 3.0 │
    │ B ┆ 12.0 │
    └──────────┴─────────────────┘



    Any ideas on where the issue? thanks in advance.

    Continue reading...

Compartilhe esta Página