Can I use newly created variables in the following expressions in `polars`?
10:23 07 Jun 2023

In R (and in particular in dplyr::mutate()), I'm used to use newly created variables in the following expressions, like so:

library(dplyr, warn.conflicts = FALSE)

head(iris) |> 
  mutate(
    sp1 = Sepal.Length + 1,
    sp2 = sp1 + 1
  )
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species sp1 sp2
#> 1          5.1         3.5          1.4         0.2  setosa 6.1 7.1
#> 2          4.9         3.0          1.4         0.2  setosa 5.9 6.9
#> 3          4.7         3.2          1.3         0.2  setosa 5.7 6.7
#> 4          4.6         3.1          1.5         0.2  setosa 5.6 6.6
#> 5          5.0         3.6          1.4         0.2  setosa 6.0 7.0
#> 6          5.4         3.9          1.7         0.4  setosa 6.4 7.4

I'm now trying to learn polars and it seems I can't reproduce this behaviour (I'm using the Python version here to stick to the source as close as possible since the R version is not very complete yet):

import polars as pl

df = pl.DataFrame({"nrs": [1, 2, 3, None, 5]})

mod = df.with_columns(
    (pl.col("nrs") + 1).alias("nrs+1"),
    (pl.col("nrs+1") + 1).alias("nrs+2")
)
Traceback (most recent call last):
  File "", line 6, in 
    mod = df.with_columns(
          ^^^^^^^^^^^^^^^^
  File "", line 7260, in with_columns
    .collect(no_optimization=True)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "", line 1501, in collect     
    return wrap_df(ldf.collect())
                   ^^^^^^^^^^^^^
exceptions.ColumnNotFoundError: nrs+1

pip show polars:

Name: polars
Version: 0.18.0

Is this feature unavailable with polars or am I missing something?

python dataframe python-polars