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

let pl.Expr.reshape optionally take a tuple or 2 scalars rather than just a tuple #19034

Open
deanm0000 opened this issue Sep 30, 2024 · 0 comments
Labels
enhancement New feature or an improvement of an existing feature needs decision Awaiting decision by a maintainer

Comments

@deanm0000
Copy link
Collaborator

Description

Most methods in polars (with_columns, select, group_by, etc) take *args and are flexible to using that or inputting a Sequence. reshape is the outlier in that it specifically requires a tuple. Most times, for me anyway, I just want the rows to be inferred so it'd be nicer to do pl.col('a').reshape(width) rather than pl.col('a').reshape((width, -1)). In comparison to numpy, its reshape accepts an int as the shape not just a tuple.

I'd like to redefine the function to

from polars.exceptions import InvalidOperationError
def reshape(self, dimensions: tuple[int, int] | int, *args: tuple[int, ...]) -> Expr:
    if isinstance(dimensions, int):
        if len(args)==0:
            dimensions=(dimensions, -1)
        elif len(args)==1:
            dimensions=(dimensions, args[0])
        else:
            msg="reshape only takes 2 dimensions"
            raise InvalidOperationError(msg)
    else:
        if len(dimensions)!=2:
            msg="When dimensions is tuple, it must be 2 elements wide"
            raise InvalidOperationError(msg)
        if len(args)>0:
            msg="When dimensions is tuple, extra arguments are ignored"
            issue_warning(msg,...)
        
    return self._from_pyexpr(self._pyexpr.reshape(dimensions)) 
@deanm0000 deanm0000 added enhancement New feature or an improvement of an existing feature needs decision Awaiting decision by a maintainer labels Sep 30, 2024
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 needs decision Awaiting decision by a maintainer
Projects
None yet
Development

No branches or pull requests

1 participant