Skip to content

Commit 184cd3c

Browse files
fix: avoid in-place modification of groups in rebinning (#1047)
* Fix: avoid in-place modification of groups in rebinning (use [1, *groups]) * Test: ensure rebinning does not mutate input groups list in-place * style: pre-commit fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 69af3cf commit 184cd3c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/boost_histogram/histogram.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ def __getitem__(self, index: IndexingExpr) -> Self | float | Accumulator:
10671067
new_j_base = 0
10681068

10691069
if old_axis.traits_underflow and axes[i].traits_underflow:
1070-
groups.insert(0, 1)
1070+
groups = [1, *groups]
10711071
elif axes[i].traits_underflow:
10721072
new_j_base = 1
10731073

tests/test_histogram_indexing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,3 +554,19 @@ def test_setting_histogram_mismatch():
554554
h[:len, 0] = bh.Histogram(
555555
bh.axis.Regular(10, 0, 10, underflow=False, overflow=False)
556556
)
557+
558+
559+
def test_rebin_groups_no_inplace_modification():
560+
"""
561+
Test that rebinning with a groups list does not mutate the input list in-place.
562+
This ensures consecutive rebin operations with the same list do not fail.
563+
"""
564+
h1 = bh.Histogram(bh.axis.Regular(60, 0, 600))
565+
h2 = bh.Histogram(bh.axis.Regular(60, 0, 600))
566+
rebinner = [5] * 12
567+
h3 = h1[bh.rebin(groups=rebinner)]
568+
# The original list should remain unchanged
569+
assert rebinner == [5] * 12, "rebinner list was mutated in-place"
570+
# Second rebin should not raise
571+
h4 = h2[bh.rebin(groups=rebinner)]
572+
assert h3 == h4

0 commit comments

Comments
 (0)