boost-histogram icon indicating copy to clipboard operation
boost-histogram copied to clipboard

[BUG] Cannot get index of huge numbers in IntCategory

Open LarsKolk opened this issue 3 years ago • 1 comments

I ran into the problem that trying to access the index of huge number in IntCategory via .index leads to an key-error, saying that the value does not exist in this axis. From what I have tested, this seems to happen for numbers greater than 99_999_000. The following example works as expected in version 1.2.1, but fails in version 1.3.1. Steps to reproduce

import boost_histogram as bh
h = bh.Histogram(bh.axis.IntCategory([4, 8, 15, 16, 23, 42, 99_999_001, 1_000_010_020]))
print(f"value = {h.axes[0].value(6)}")
print(f"index = {h.axes[0].index(99_999_001)}")

Expected output:

value = 9999001
index = 6

Actual output:

value = 99999001

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [29], in <cell line: 2>()
      1 print(f"value = {h.axes[0].value(6)}")
----> 2 print(f"index = {h.axes[0].index(99_999_001)}")

File ~/.conda/envs/Test/lib/python3.8/site-packages/boost_histogram/_internal/axis.py:118, in Axis.index(self, value)
    115     msg = f"index({value}) cannot be a string for a numerical axis"
    116     raise TypeError(msg)
--> 118 return self._ax.index(value)

KeyError: '99999001 not in axis'

LarsKolk avatar Apr 25 '22 12:04 LarsKolk

Very strange! I’ll try to look at it soon (possibly after PyCon).

henryiii avatar Apr 26 '22 05:04 henryiii