Skip to content

bug: when combines multi-output expressions (but shouldn't) #3029

@MarcoGorelli

Description

@MarcoGorelli
df = pd.DataFrame({'a': [1, 2, 3], 'b': [0,1,6]})
print(nw.from_native(df).select(nw.when(nw.col('a', 'b')>1).then(999).otherwise(666)))

df = pl.DataFrame({'a': [1, 2, 3], 'b': [0,1,6]})
print(df.select(pl.when(pl.col('a', 'b')>1).then(999).otherwise(666)))

Narwhals returns

┌──────────────────┐
|Narwhals DataFrame|
|------------------|
|       literal    |
|    0      666    |
|    1      666    |
|    2      999    |
└──────────────────┘

but Polars expands the predicate and ends up with

polars.exceptions.DuplicateError: the name 'literal' is duplicate

It's possible that multiple expressions are returning the same default column name. If this is the case, try renaming the columns with `.alias("new_name")` to avoid duplicate column names.

As a consequence, Polars allows

pl.when(pl.all()>1).then(pl.all())

but we don't

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions