Код: Выделить всё
import polars as pl
x = pl.DataFrame(
{
'A': [1., None, 3.],
'B': [4., 5., 6.],
'C': [7., 8., None],
}
)
x.with_columns(
pl.when(pl.sum_horizontal('B', 'C') > 12)
.then(pl.sum_horizontal(pl.col('A', 'B')))
.otherwise(None)
.alias('A+B when B+C>12 (expected)'),
pl.when(pl.sum_horizontal('B', 'C') > 12)
.then(pl.concat_list(pl.col('A', 'B')).list.sum())
.otherwise(None)
.alias('A+B when B+C>12 (odd)'),
pl.concat_list(pl.exclude('C')).list.sum().alias('A+B'),
)
Код: Выделить всё
┌──────┬─────┬──────┬────────────────────────────┬───────────────────────┬─────┐
│ A ┆ B ┆ C ┆ A+B when B+C>12 (expected) ┆ A+B when B+C>12 (odd) ┆ A+B │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ list[f64] ┆ f64 │
╞══════╪═════╪══════╪════════════════════════════╪═══════════════════════╪═════╡
│ 1.0 ┆ 4.0 ┆ 7.0 ┆ null ┆ null ┆ 5.0 │
│ null ┆ 5.0 ┆ 8.0 ┆ 5.0 ┆ [5.0] ┆ 5.0 │
│ 3.0 ┆ 6.0 ┆ null ┆ null ┆ null ┆ 9.0 │
└──────┴─────┴──────┴────────────────────────────┴───────────────────────┴─────┘
Это естественно для функций агрегирования, таких как .list.sum() всегда возвращать одно значение, отличное от списка?
Подробнее здесь: https://stackoverflow.com/questions/785 ... ible-issue