Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support comparison operations for list types #18873

Open
jdcla opened this issue Sep 23, 2024 · 2 comments
Open

Support comparison operations for list types #18873

jdcla opened this issue Sep 23, 2024 · 2 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@jdcla
Copy link

jdcla commented Sep 23, 2024

Description

Support comparison operations for list types. This feature would circumvent the subsequent use of explode() and agg(), which is memory intensive.

    df = df = pl.DataFrame({
        'x1': [[1,2],[3,4]],
        'x2': [1,4]
    })

    df.with_columns(
        out = pl.col('x1').list.ge(pl.col('x2'))
    )
# OR
    df.with_columns(
        out = pl.col('x1') >= pl.col('x2')
    )

    # Desired output: 
    shape: (2, 3)
    ┌───────────┬─────┬───────────────┐
    │ x1x2out           │
    │ ---------           │
    │ list[i64] ┆ i64list[f64]     │
    ╞═══════════╪═════╪═══════════════╡
    │ [1, 2]    ┆ 1   ┆ [True, True]  │
    │ [3, 4]    ┆ 4   ┆ [False, True] │
    └───────────┴─────┴───────────────┘

Trying to run pl.col('x1') >= pl.col('x2') or pl.col('x1').ge(pl.col('x2')) throws a SchemaError, referencing the difference in types for x1 and x2.

@jdcla jdcla added the enhancement New feature or an improvement of an existing feature label Sep 23, 2024
@thomasfrederikhoeck
Copy link
Contributor

I guess this is very similar to #10918

@cmdlineluser
Copy link
Contributor

List -> List arithmetic was part of today's 1.8 release thanks to the work in #17823

df.with_columns(pl.col.x1 - pl.col.x2.repeat_by(pl.col.x1.list.len()))
# shape: (2, 2)
# ┌───────────┬─────┐
# │ x1        ┆ x2  │
# │ ---       ┆ --- │
# │ list[i64] ┆ i64 │
# ╞═══════════╪═════╡
# │ [0, 1]    ┆ 1   │
# │ [-1, 0]   ┆ 4   │
# └───────────┴─────┘

Comparisons panic though - not sure if there have been discussed yet?

df.with_columns(pl.col.x1 > pl.col.x2.repeat_by(pl.col.x1.list.len()))
# thread '<unnamed>' panicked at crates/polars-core/src/chunked_array/comparison/mod.rs:690:9:
# not implemented
# PanicException: not implemented

I think all the different behaviours have popped up across a few different previous feature requests.

The latest updates I have seen are here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants