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

Allow polars.Expr.round to get negative decimals #20028

Open
herrmann1981 opened this issue Nov 27, 2024 · 2 comments
Open

Allow polars.Expr.round to get negative decimals #20028

herrmann1981 opened this issue Nov 27, 2024 · 2 comments
Labels
enhancement New feature or an improvement of an existing feature

Comments

@herrmann1981
Copy link

Description

Sometimes I want to round values not to a decimal position after the comma but before the comma. This is currently not possible with the round function.

df = pl.DataFrame({'number': [12, 15, 13.6, 13.69, 13.98456]}, schema={'number': pl.Float32})
df.with_columns(rounded=pl.col('number').round(-1))

This currently results in the following error:
OverflowError: can't convert negative int to unsigned

What I would expect is something like:

df.with_columns(rounded=pl.col('number').round(-1))

| number | rounded |
| --- |--- │
| f32 | f32 |
| | |
| 12.0 | 10.0 │
| 15.0 | 16.0 │
| 13.6 | 10.0 │
| 13.69 | 10.0 │
| 13.98456 | 10.0 │

@herrmann1981 herrmann1981 added the enhancement New feature or an improvement of an existing feature label Nov 27, 2024
@Julian-J-S
Copy link
Contributor

I agree that this is useful and python has native support for that.

There are multiple "hacks" to achieve this also with the current methods e.g.:

  • 1_234: round with -2
  • divide by 10**2: 1234 / 100 = 12.34
  • round(0) or cast to int -> 12
  • multiply by 10**2 -> 1200 😉

@herrmann1981
Copy link
Author

@Julian-J-S Thanks for the fast response. Yes I know about this workaround. I just thought it would be useful to have the shortcut in the framework.

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

2 participants