Skip to content

hist and bin produce zero-width bins for a constant coord #3935

Open

Description

@SimonHeybrock

hist(x=N) and bin(x=N) derive the edges from the coord range. For a constant coord that range is empty, and the result is N bins of which N-1 have zero width:

import scipp as sc

da = sc.DataArray(
    sc.ones(dims=['row'], shape=[5], unit='counts'),
    coords={'x': sc.array(dims=['row'], values=[3.0] * 5, unit='K')},
)
h = da.hist(x=4)
h.coords['x'].values  # array([3., 3., 3., 3.]) -- top edge raised by one ULP
h.values              # array([0., 5., 0., 0.])

_upper_bound raises the top edge by np.nextafter (by one for integer and datetime coords), which keeps the range technically non-empty, and linspace then spreads N+1 edges over it. All the counts land in bin index 1, whatever N. bin behaves the same way.

Integer coords show it a little differently — edges [7, 7, 7, 7, 8] for x=4 — where the whole unit of the bump lands in the last bin.

scipp 26.3.1, numpy 2.4.6.

Expected

Some defined behaviour for an empty coord range. Raising seems defensible, since "N bins spanning this range" is unsatisfiable; widening the range to something representable, or returning a single bin, would work too. The case worth avoiding is the current one, where the result looks well-formed and sum() is correct, so nothing surfaces the problem until you inspect the edges or try to draw them.

Activity

  1. changed the title [-]hist and bin produce zero-width bins for a constant coord[/-] [+]`hist` and `bin` produce zero-width bins for a constant coord[/+] on Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions