From c3789b0e0d351aea41e293c09518fa1f1275386b Mon Sep 17 00:00:00 2001 From: Lindsey Gray Date: Wed, 23 Dec 2020 21:55:48 -0600 Subject: [PATCH 1/2] allow weights to be single numbers like values --- coffea/hist/hist_tools.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/coffea/hist/hist_tools.py b/coffea/hist/hist_tools.py index 738c43a33..1ee2ebcc0 100644 --- a/coffea/hist/hist_tools.py +++ b/coffea/hist/hist_tools.py @@ -954,6 +954,8 @@ def fill(self, **values): weight = values.pop("weight", None) if isinstance(weight, (awkward1.Array, np.ndarray)): weight = coffea.util._ensure_flat(weight) + if isinstance(weight, numbers.Number): + weight = np.atleast_1d(weight) if not all(d.name in values for d in self._axes): missing = ", ".join(d.name for d in self._axes if d.name not in values) raise ValueError("Not all axes specified for %r. Missing: %s" % (self, missing)) From 1223968e3823ad7f6bbe1ae8bffc0fe7845a25d5 Mon Sep 17 00:00:00 2001 From: Lindsey Gray Date: Wed, 23 Dec 2020 21:58:19 -0600 Subject: [PATCH 2/2] add test for the problem cited in 394 --- tests/test_hist_tools.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_hist_tools.py b/tests/test_hist_tools.py index 0d8d61d3f..fec8d00b0 100644 --- a/tests/test_hist_tools.py +++ b/tests/test_hist_tools.py @@ -265,3 +265,7 @@ def test_issue_333(): axis = hist.Bin("channel", "Channel b1", 50, 0, 2000) temp = np.arange(0, 2000, 40, dtype=np.int16) assert np.all(axis.index(temp) == np.arange(50) + 1) + +def test_issue_394(): + dummy = hist.Hist("Dummy" , hist.Cat("sample", "sample"), hist.Bin("dummy", "Number of events", 1, 0, 1)) + dummy.fill(sample="test", dummy=1, weight=0.5)