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

fix: Ensure floating-point accuracy in hist #22245

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

Conversation

mcrumiller
Copy link
Contributor

@mcrumiller mcrumiller commented Apr 12, 2025

Closes #22234

Two minor adjustments:

  1. When user-supplied bins are provided, the slow path is always taken. This guarantees that the user's bins are always respected. The prior implementation attempted to detect if the bins were uniformly distributed using a heuristic, but that heuristic can be foiled, and we were bound to encounter an issue on this sometime in the future.

  2. In the fast path (now only when bin_count is supplied), the index calculation might generate results > 1 ULP of the bin edges in some cases, resulting in an invalid index, as see in the linked issue. There, we have 1.0 - 1e-17 == 1.0:

    >>> import polars as pl
    >>> pl.Series([1.0]) - 1e-17 == 1.0
    shape: (1,)
    Series: '' [bool]
    [
            true
    ]

    There is now a check to see whether the value should be moved to an adjacent bucket. This check encompasses a prior check, so performance should not be affected.

@github-actions github-actions bot added fix Bug fix python Related to Python Polars rust Related to Rust Polars labels Apr 12, 2025
Copy link

codecov bot commented Apr 12, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 80.77%. Comparing base (69ad1ba) to head (e3f710d).

Files with missing lines Patch % Lines
crates/polars-ops/src/chunked_array/hist.rs 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #22245      +/-   ##
==========================================
- Coverage   80.78%   80.77%   -0.01%     
==========================================
  Files        1639     1639              
  Lines      235548   235538      -10     
  Branches     2714     2714              
==========================================
- Hits       190278   190261      -17     
- Misses      44630    44637       +7     
  Partials      640      640              

☔ 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.

@mcrumiller mcrumiller marked this pull request as ready for review April 12, 2025 16:28
@mcrumiller mcrumiller changed the title fix: Ensure hist accuracy in the face of floating-point imprecision fix: Ensure floating-point accuracy in hist Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Series.hist precision loss near 0 for small floats
1 participant