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

feat: add nw.Struct , nw.List, and nw.Array dtypes #1067

Closed
MarcoGorelli opened this issue Sep 24, 2024 · 3 comments
Closed

feat: add nw.Struct , nw.List, and nw.Array dtypes #1067

MarcoGorelli opened this issue Sep 24, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@MarcoGorelli
Copy link
Member

Polars has these natively

In pandas, they're available after pandas 2.0.0 with PyArrow dtypes, e.g.:

In [21]: pd.Series([[1, 2], [3, 4, 5]], dtype=pd.ArrowDtype(pa.list_(pa.int64())))
Out[21]:
0      [1 2]
1    [3 4 5]
dtype: list<item: int64>[pyarrow]

For pandas, I think it's OK to require pyarrow when casting to nw.List / nw.Struct / nw.Array

@MarcoGorelli MarcoGorelli added the enhancement New feature or request label Sep 24, 2024
@FBruzzesi
Copy link
Member

For motivation... plotly has the following aggregation in group by:

def aggfunc_discrete(x):
    uniques = nw.col(x).unique()
    if uniques.len() == 1:
        return uniques[0]
     else:
        return "(?)"

In the current status it would be hard for us to replicate. However if we manage to add List type and namespace then we could approach it as:

(df
.group_by("a")
.agg(nw.col("b").unique())
.select(
    nw.when(nw.col("b").list.len() == 1).then(nw.col("b").list.first()).otherwise("(?)")
 )
)

It's late so maybe I am missing something simpler than this snippet.

I think we should figure out how to do it in pandas without pyarrow backend via duck typing and raise a type/attribute error if we are not able to perform a list computation.

@MarcoGorelli
Copy link
Member Author

i'm not sure about allowing non-aggregations in Narwhals, but regarding the unique issue raised here, I have a suggestion in #1076 (comment)

@MarcoGorelli
Copy link
Member Author

These are all available in Narwhals now so we can close this

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

No branches or pull requests

2 participants