Skip to content

[BUG] Indexing with negative numbers #559

@andrzejnovak

Description

@andrzejnovak

Describe the bug

hist doesn't process negative indexing on axes.

Steps to reproduce

import hist
import numpy as np

h1 = hist.new.Reg(10, 0, 10).Weight().fill(np.random.normal(5, 2, int(1e6)))
# Works fine
h1[1:4]

# Throws an error
h1[1:-2]

Trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 1
----> 1 h1[1:-2]

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/hist/basehist.py:324, in BaseHist.__getitem__(self, index)
    317 def __getitem__(  # type: ignore[override]
    318     self: T, index: IndexingExpr
    319 ) -> T | float | bh.accumulators.Accumulator:
    320     """
    321     Get histogram item.
    322     """
--> 324     return super().__getitem__(self._index_transform(index))

File ~/software/micromamba/envs/def/lib/python3.11/site-packages/boost_histogram/_internal/hist.py:908, in Histogram.__getitem__(self, index)
    906         assert isinstance(start, int)
    907         assert isinstance(stop, int)
--> 908         slices.append(_core.algorithm.slice_and_rebin(i, start, stop, merge))
    910 # Will be updated below
    911 if slices or pick_set or pick_each or integrations:

ValueError: begin < end required

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