Skip to content

Commit 513585e

Browse files
authored
fix: access getitem via string was broken (and attr access uses this too) (#487)
1 parent 41f56ef commit 513585e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/boost_histogram/_internal/view.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def __str__(self):
3737
)
3838

3939
def __setitem__(self, ind, value):
40+
# `.value` really is ["value"] for an record array
41+
if isinstance(ind, str):
42+
super(View, self).__setitem__(ind, value)
43+
return
44+
4045
array = np.asarray(value)
4146
if (
4247
array.ndim == super(View, self).__getitem__(ind).ndim + 1

tests/test_storage.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,17 @@ def test_setting_weighted_profile():
201201
a.view()._sum_of_weighted_deltas_squared,
202202
b.view()["_sum_of_weighted_deltas_squared"],
203203
)
204+
205+
206+
# Issue #486
207+
def test_modify_weights_by_view():
208+
bins = [0, 1, 2]
209+
hist = bh.Histogram(bh.axis.Variable(bins), storage=bh.storage.Weight())
210+
yields = [3, 4]
211+
var = [0.1, 0.2]
212+
hist[...] = np.stack([yields, var], axis=-1)
213+
214+
hist.view().value /= 2
215+
216+
assert hist.view().value[0] == pytest.approx(1.5)
217+
assert hist.view().value[1] == pytest.approx(2)

0 commit comments

Comments
 (0)