pythonjupyter-notebookpython-polars

Why won't polars.when and then run in jupyters notebook?


I'm trying to insert/make a new column with these attributes in lines of then("") but i get columnNotFoundError instead for the lines of .then(). But these does not exists yet, what is wrong with the code?

    df_2023 = df_2023.with_columns(
            pl.when(pl.col("Tillatt totalvekt opp til og med 3500").eq("X"))
            .then("Opp til og med 3500")
            .when(pl.col("Tillatt totalvekt 3501-7500").eq("X"))
            .then("3501-7500")
            .when(pl.col("Tillatt totalvekt over 7500").eq("X"))
            .then("Over 7500")
            .otherwise(pl.lit(None).cast(pl.String))
            .alias("Tillatt totalvekt")
    )
    
    df_2023.select("Tillat totalvekt").head()
    
    
    *THE ERROR*
    
    *ColumnNotFoundError: Opp til og med 3500*
    

I also tried with:

pl.when(pl.col("Tillatt totalvekt opp til og med 3500") == "X")
    .then("Opp til og med 3500")
    

I could really use some help, read also the Polars documentation for polars.when, but it was for no help to solve this problem.

Thanks in advance!


Solution

  • The then construct assumes strings are column names. I guess "Opp til og med 3500" is a literal value in your code and not a column name. Use pl.lit('....') to define a literal value explicitly, that way polars won't consider it as a column name. In your case, .then(pl.lit("Opp til og med 3500")) (same for other then).

    Some more details: https://github.com/pola-rs/polars/issues/13805