In Polars, the select
and with_column
methods broadcast any scalars that they get, including literals:
import polars as pl
df.with_column(pl.lit(1).alias("y"))
# shape: (3, 2)
# ┌─────┬─────┐
# │ x ┆ y │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1 ┆ 1 │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 2 ┆ 1 │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 3 ┆ 1 │
# └─────┴─────┘
The agg
method does not broadcast literals:
import polars as pl
df = pl.DataFrame(dict(x=[1,1,0,0])).groupby("x")
df.agg(pl.lit(1).alias("y"))
# exceptions.ComputeError: returned aggregation is a different length: 1 than the group lengths: 2
Is there an operation I can apply that will broadcast a scalar and ignore a non-scalar? Something like this:
df.agg(something(pl.lit(1)).alias("y"))
# shape: (2, 2)
# ┌─────┬─────┐
# │ x ┆ y │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 0 ┆ 1 │
# ├╌╌╌╌╌┼╌╌╌╌╌┤
# │ 1 ┆ 1 │
# └─────┴─────┘