How do you access a column name in a polars expression?
03:59 17 May 2023

I implemented a sigmoid transformation in polars as follows:

def sigmoid(c:pl.Expr)->pl.Expr:
    return 1 / ((-c).exp() + 1)

This works great, except that by polars naming conventions the resulting column is called 'literal'

I could keep the column name by re-writing sigmoid as

def sigmoid(c:pl.Expr)->pl.Expr:
    return ((c * -1).exp() + 1)**-1

But: A. That is horrible B. I don't want my code to have this "magical/invisible" tracking of column names

What I'd like to do is add a .alias() at the end of my function to ensure the column name is preserved.

The following pseudo-code expresses the idea:

def sigmoid(c:pl.Expr)->pl.Expr:
    return (1 / ((-c).exp() + 1)).alias(c.name)

However, polars expressions do not have a .name attribute.

How else could I keep the column name?

Not that I could do:

df.select(
   pl.col('a').pipe(sigmoid).alias('a'), 
   pl.col('b').pipe(sigmoid).alias('b'), 
   pl.col('c').pipe(sigmoid).alias('c'), 
   ...
)

But that is cumbersome, and would not work well with

df.select(
   pl.all().pipe(sigmoid)
)
python dataframe python-polars