Код: Выделить всё
d1 = pl.DataFrame({
'x': ['a', None, 'b'],
'y': [1.1, 2.2, 3.3]
})
print(d1)
shape: (3, 2)
┌──────┬─────┐
│ x ┆ y │
│ --- ┆ --- │
│ str ┆ f64 │
╞══════╪═════╡
│ a ┆ 1.1 │
│ null ┆ 2.2 │
│ b ┆ 3.3 │
└──────┴─────┘
Код: Выделить всё
# Using [print(d), 999][-1] hack just to trace calls
d2 = d1.select(
pl.when(pl.col('x').is_not_null())
.then(pl.struct(['x','y']).apply(lambda d: [print(d), 999][-1]))
.otherwise(None)
.alias('s'))
print(d2)
Код: Выделить всё
{'x': 1, 'y': 1.1}
{'x': None, 'y': 2.2}
Подробнее здесь: [url]https://stackoverflow.com/questions/76774163/why-is-polars-running-my-then-function-even-if-the-when-condition-is-false[/url]