pola-rs / polars

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

Multiplication not allowed for bool dtype #19919

Closed jesusestevez closed 6 days ago

jesusestevez commented 6 days ago

Checks

Reproducible example

import polars as pl

df = pl.DataFrame(
    {"sets": [["a", "b"], ["a", "b"], ["a", "b"]], 
     "optional_members": [["a", "b"], ["a", "b"], ["a", "c"]],
     "exposure": [[1, 2], [3, 4], [5, 6]]}
)

lookup = df.with_columns(
    pl.col.sets.explode().is_in(pl.col.optional_members.explode())
    .implode()
    .over(pl.int_range(pl.len()))
    .alias("lookup_result")
)

lookup.with_columns(pl.col("lookup_result") * pl.col("exposure"))

Log output

No response

Issue description

Following #8006 , I would expect list multiplication to be allowed for boolean dtypes, behaving as the list division. However, it seems it is not supported. Is there any reason for this behaviour?

Expected behavior

I would expect the multiplication (when one of the columns is bool dtype, to behave as the division:

import polars as pl

df = pl.DataFrame(
    {"sets": [["a", "b"], ["a", "b"], ["a", "b"]], 
     "optional_members": [["a", "b"], ["a", "b"], ["a", "c"]],
     "exposure": [[1, 2], [3, 4], [5, 6]]}
)

lookup = df.with_columns(
    pl.col.sets.explode().is_in(pl.col.optional_members.explode())
    .implode()
    .over(pl.int_range(pl.len()))
    .alias("lookup_result")
)

lookup.with_columns(pl.col("lookup_result") / pl.col("exposure"))

Installed versions

``` Replace this line with the output of pl.show_versions(). Leave the backticks in place. ```
cmdlineluser commented 6 days ago

It doesn't mention what error you get or the pl.show_versions() output, but this is what I get on 1.14.0

lookup.with_columns(pl.col("lookup_result") * pl.col("exposure"))
# shape: (3, 4)
# ┌────────────┬──────────────────┬───────────┬───────────────┐
# │ sets       ┆ optional_members ┆ exposure  ┆ lookup_result │
# │ ---        ┆ ---              ┆ ---       ┆ ---           │
# │ list[str]  ┆ list[str]        ┆ list[i64] ┆ list[i64]     │
# ╞════════════╪══════════════════╪═══════════╪═══════════════╡
# │ ["a", "b"] ┆ ["a", "b"]       ┆ [1, 2]    ┆ [1, 2]        │
# │ ["a", "b"] ┆ ["a", "b"]       ┆ [3, 4]    ┆ [3, 4]        │
# │ ["a", "b"] ┆ ["a", "c"]       ┆ [5, 6]    ┆ [5, 0]        │
# └────────────┴──────────────────┴───────────┴───────────────┘
jesusestevez commented 6 days ago

I missed an update. Thanks for the feedback!