polars.Expr.keep_name#

Expr.keep_name() Expr[source]#

Keep the original root name of the expression.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 2],
...         "b": [3, 4],
...     }
... )

Keep original column name to undo an alias operation.

>>> df.with_columns([(pl.col("a") * 9).alias("c").keep_name()])
shape: (2, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 9   ┆ 3   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 18  ┆ 4   │
└─────┴─────┘

Prevent “DuplicateError: Column with name: ‘literal’ has more than one occurrences” errors.

>>> df.select([(pl.lit(10) / pl.all()).keep_name()])
shape: (2, 2)
┌──────┬──────────┐
│ a    ┆ b        │
│ ---  ┆ ---      │
│ f64  ┆ f64      │
╞══════╪══════════╡
│ 10.0 ┆ 3.333333 │
├╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
│ 5.0  ┆ 2.5      │
└──────┴──────────┘