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

float and decimal have different round behavior (round up; half even) #20033

Open
Julian-J-S opened this issue Nov 27, 2024 · 1 comment
Open
Labels
enhancement New feature or an improvement of an existing feature

Comments

@Julian-J-S
Copy link
Contributor

Description

pl.DataFrame(
    {"d": ["0.5", "1.5"], "f": [0.5, 1.5]},
    schema_overrides={"d": pl.Decimal},
).with_columns(
    pl.all().round(decimals=0).name.suffix("_0"),
)


shape: (2, 4)
┌──────────────┬─────┬──────────────┬─────┐
│ dfd_0f_0 │
│ ------------ │
│ decimal[*,1] ┆ f64decimal[*,1] ┆ f64 │
╞══════════════╪═════╪══════════════╪═════╡
│ 0.50.50.01.0 │
│ 1.51.52.02.0 │
└──────────────┴─────┴──────────────┴─────┘
  • 0.5 -> 0.0: decimal
  • 0.5 -> 1.0: float (note: both 0.5 and 1.5 have EXACT representation as float)

Request:

  • same/consistent round behavior across types
  • (BONUS): configurable round behavior ⭐
@Julian-J-S Julian-J-S added the enhancement New feature or an improvement of an existing feature label Nov 27, 2024
@syedtaz
Copy link

syedtaz commented Nov 30, 2024

Hi.

The polars-ops/series/ops/round module uses the default floating point round() function (which rounds up) and implements banker's rounding for decimals. Rust's stdlib includes round_ties_even() since 1.77 -- switching the functions around seems to work.

>>> import polars as pl
>>> pl.DataFrame({"d": ["0.5", "1.5"], "f": [0.5, 1.5]}, schema_overrides={"d": pl.Decimal}).with_columns(pl.all().round(decimals=0).name.suffix("_0")
... )
shape: (2, 4)
┌──────────────┬─────┬──────────────┬─────┐
│ dfd_0f_0 │
│ ------------ │
│ decimal[*,1] ┆ f64decimal[*,1] ┆ f64 │
╞══════════════╪═════╪══════════════╪═════╡
│ 0.50.50.00.0 │
│ 1.51.52.02.0 │
└──────────────┴─────┴──────────────┴─────┘

I am happy to work on this feature properly and implement the configurable rounding behavior if this enhancement is going to be accepted. I am not sure if there is some code out there that depends on the rounding-up behavior -- the docs for polars.Series.round at least don't seem to commit to anything.

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