Skip to content

feat: Add RoundMode for Decimal and Float #22248

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Julian-J-S
Copy link
Contributor

fix #21800 (though as @orlp mentioned there are theoretically more round modes to add)

Current Problem ⚠️

  • current round expression has no mode and is inconsistent
    • Decimal: uses "half to even"
    • Float: uses "half away from zero"

PR Solution 🍀

  • implement missing round modes
    • Decimal: implement "half away from zero"
    • Float: implement "half to even"
  • set "half to even" as default for float & decimal

Example

df = pl.DataFrame(
    {
        "f64": [-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5],
        "d": ["-3.5", "-2.5", "-1.5", "-0.5", "0.5", "1.5", "2.5", "3.5"],
    },
    schema={
        "f64": pl.Float64,
        "d": pl.Decimal(scale=1),
    },
)

df.with_columns(
    pl.all().round().name.suffix("_default"),
    pl.all().round(mode="half_away_from_zero").name.suffix("_away_from_zero"),
    pl.all().round(mode="half_to_even").name.suffix("_to_even"),
)

shape: (8, 8)
┌──────┬──────────────┬─────────────┬──────────────┬────────────────────┬──────────────────┬─────────────┬──────────────┐
│ f64df64_defaultd_defaultf64_away_from_zerod_away_from_zerof64_to_evend_to_even    │
│ ------------------------          │
│ f64decimal[*,1] ┆ f64decimal[*,1] ┆ f64decimal[*,1]     ┆ f64decimal[*,1] │
╞══════╪══════════════╪═════════════╪══════════════╪════════════════════╪══════════════════╪═════════════╪══════════════╡
│ -3.5-3.5-4.0-4.0-4.0-4.0-4.0-4.0         │
│ -2.5-2.5-2.0-2.0-3.0-3.0-2.0-2.0         │
│ -1.5-1.5-2.0-2.0-2.0-2.0-2.0-2.0         │
│ -0.5-0.5-0.00.0-1.0-1.0-0.00.0          │
│ 0.50.50.00.01.01.00.00.0          │
│ 1.51.52.02.02.02.02.02.0          │
│ 2.52.52.02.03.03.02.02.0          │
│ 3.53.54.04.04.04.04.04.0          │
└──────┴──────────────┴─────────────┴──────────────┴────────────────────┴──────────────────┴─────────────┴──────────────┘

Open

  • there are other round modes that can be implemented later

@orlp @wence-
sorry for creating another PR (old: #21883). I was overwhelmed and stuck in merge/rebase hell.
I implemented your requests. Please have a look 🤓

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Apr 13, 2025
Copy link

codecov bot commented Apr 13, 2025

Codecov Report

Attention: Patch coverage is 56.64336% with 62 lines in your changes missing coverage. Please review.

Project coverage is 80.77%. Comparing base (69ad1ba) to head (f1832d4).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
crates/polars-ops/src/series/ops/round.rs 51.75% 55 Missing ⚠️
crates/polars-python/src/conversion/mod.rs 50.00% 5 Missing ⚠️
.../polars-python/src/lazyframe/visitor/expr_nodes.rs 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #22248      +/-   ##
==========================================
- Coverage   80.78%   80.77%   -0.01%     
==========================================
  Files        1639     1639              
  Lines      235548   235618      +70     
  Branches     2714     2714              
==========================================
+ Hits       190278   190322      +44     
- Misses      44630    44655      +25     
- Partials      640      641       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add RoundMode to round and fix rounding inconsistency (Decimal uses "half_even", float uses "up")
1 participant