narwhals-dev / narwhals

Lightweight and extensible compatibility layer between dataframe libraries!
https://narwhals-dev.github.io/narwhals/
MIT License
423 stars 76 forks source link

[Enh]: Support `Expr.len()` #322

Closed FBruzzesi closed 3 months ago

FBruzzesi commented 3 months ago

We would like to learn about your use case. For example, if this feature is needed to adopt Narwhals in an open source project, could you please enter the link to it below?

No response

Please describe the purpose of the new feature or describe the problem to solve.

nw.len() is not enough is some situation. Here a reproducible example in polars:

import polars as pl

df = pl.DataFrame({"a": [-1.5, -.5, 0.5, 1.5, 2.5], "b": [1,2,1,2,1,]})
df.select(
    pl.col("a").filter(pl.col("b")==1).len().alias("1"),
    pl.col("a").filter(pl.col("b")==2).len().alias("2"),
)
shape: (1, 2)
┌─────┬─────┐
│ 1   ┆ 2   │
│ --- ┆ --- │
│ u32 ┆ u32 │
╞═════╪═════╡
│ 3   ┆ 2   │
└─────┴─────┘

Suggest a solution if possible.

No response

If you have tried alternatives, please describe them below.

No response

Additional information that may help us understand your needs.

No response