pola-rs / polars

Dataframes powered by a multithreaded, vectorized query engine, written in Rust
https://docs.pola.rs
Other
30.52k stars 1.98k forks source link

Min_horizontal should not create new field if literal comes first #19954

Closed lmocsi closed 7 hours ago

lmocsi commented 11 hours ago

Checks

Reproducible example

import polars as pl
df = pl.DataFrame({"a": [1, 8, 3]})
df.with_columns(pl.min_horizontal(5,"a"), # creates new column 'literal'
                pl.min_horizontal("a",5), # keeps results in column 'a'
                )

Log output

-

Issue description

If a number comes first in the min_horizontal arguments, then polars creates a new field called 'literal', but if an existing field comes first, then it retains the column name.

Expected behavior

Should not create new ('literal') field if only one column and one or more literals are present anywhere in the attributes. This applies to max_horizontal, as well.

┌─────┬─────────┐
│ a   ┆ literal │
│ --- ┆ ---     │
│ i64 ┆ i64     │
╞═════╪═════════╡
│ 1   ┆ 1       │
│ 5   ┆ 5       │
│ 3   ┆ 3       │
└─────┴─────────┘

Installed versions

``` --------Version info--------- Polars: 1.14.0 Index type: UInt32 Platform: Linux-5.10.223-212.873.amzn2.x86_64-x86_64-with-glibc2.26 Python: 3.9.20 | packaged by conda-forge | (main, Sep 30 2024, 17:49:10) [GCC 13.3.0] LTS CPU: False ----Optional dependencies---- adbc_driver_manager altair boto3 1.35.63 cloudpickle 3.1.0 connectorx deltalake fastexcel fsspec 2024.10.0 gevent google.auth 2.36.0 great_tables matplotlib nest_asyncio 1.6.0 numpy 1.26.4 openpyxl pandas 1.5.3 pyarrow 14.0.2 pydantic pyiceberg sqlalchemy torch xlsx2csv xlsxwriter ```
MarcoGorelli commented 11 hours ago

this looks correct to me, it takes the name from the left-most expression, which in this case is a literal