Skip to content

Handle zero interquartile range in automatic histograms - #467

Merged
mahmoud merged 1 commit into
mahmoud:masterfrom
codewithfourtix:fix-histogram-zero-interquartile-range
Sep 7, 2026
Merged

mahmoud merged 1 commit into
mahmoud:masterfrom
codewithfourtix:fix-histogram-zero-interquartile-range

Conversation

@codewithfourtix

Copy link
Copy Markdown
Contributor

Automatic histogram bins divide by zero when the interquartile range is zero, including constant datasets. Fall back to one bin spanning the data when the Freedman width is zero.

Adds regressions for constant values and a repeated value with an outlier.

Copilot AI lite review requested due to automatic review settings September 5, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change directly addresses the verified division-by-zero path and the added tests cover the reported regressions without introducing API or behavioral inconsistencies beyond the intended fallback.

Pull request overview

This pull request fixes a division-by-zero failure in Stats automatic histogram bin selection when the interquartile range is zero (e.g., constant data, or many repeated values with an outlier). It does so by detecting a zero Freedman–Diaconis bin width and falling back to a single-bin histogram, and adds regression tests to cover the affected scenarios.

Changes:

  • Add a dx == 0 guard in Freedman-based bin bound calculation to avoid dividing by zero and to produce a one-bin result.
  • Add tests covering constant datasets and a repeated-value dataset with an outlier to ensure both get_histogram_counts() and format_histogram() behave without error.
File summaries
File Description
boltons/statsutils.py Adds a zero-width Freedman fallback to produce a single histogram bin instead of dividing by zero.
tests/test_statsutils.py Adds regression coverage for zero-IQR datasets (constant values and repeated values with an outlier).
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mahmoud
mahmoud merged commit 1cdc0e9 into mahmoud:master Sep 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants