Skip to content

[BUG] Ambiguous indexing+ due to duplicate categorical axis entries #644

@alexander-held

Description

@alexander-held

Describe the bug

It is possible to define a categorical axis with repeated entries in boost_histogram:

import boost_histogram as bh

h = bh.Histogram(bh.axis.Regular(1, 0, 1), bh.axis.StrCategory(["a", "a"]))
h.fill(0.5, "a")

print(f" 0 : {h[:, 0].values()}")  #  0 : [1.]
print(f" 1 : {h[:, 1].values()}")  #  1 : [0.]

When accessing this with indexing+ via "a" in hist, the first match is returned (at least in this example):

import hist

h = hist.Hist.new.Regular(1, 0, 1).StrCat(["a", "a"], name="a").Double()
h.fill(0.5, a="a")

print(f"\"a\": {h[:, "a"].values()}")  # "a": [1.]
print(f" 0 : {h[:, 0].values()}")  #  0 : [1.]
print(f" 1 : {h[:, 1].values()}")  #  1 : [0.]

I do not know if there are use cases where duplicate categories would be needed. I ran into this during debugging. Otherwise I wonder if this should be explicitly disallowed at boost_histogram or hist level to avoid downstream ambiguity for uhi, or if some kind of warning should be raised here.

Steps to reproduce

see above

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