Skip to content

Commit cc7e4c1

Browse files
veprblhenryiii
andauthored
fix: allow filling Int axis with unsigned integers (#917)
* fix: allow filling Int axis with unsigned integers * tests: add test for uint filling Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]> Co-authored-by: Henry Schreiner <[email protected]>
1 parent e8e2487 commit cc7e4c1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

include/bh_python/fill.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ inline decltype(auto) special_cast<c_array_t<int>>(py::handle x) {
102102
auto dtype = py::cast<py::array>(x).dtype();
103103
if(dtype.equal(np.attr("bool_")) || dtype.equal(np.attr("int8"))
104104
|| dtype.equal(np.attr("int16")) || dtype.equal(np.attr("int32"))
105-
|| dtype.equal(np.attr("int64")))
105+
|| dtype.equal(np.attr("int64")) || dtype.equal(np.attr("uint8"))
106+
|| dtype.equal(np.attr("uint16")) || dtype.equal(np.attr("uint32"))
107+
|| dtype.equal(np.attr("uint64")))
106108
return py::cast<c_array_t<int>>(x);
107109
throw py::type_error("Only integer arrays supported when targeting integer axes");
108110
}

tests/test_histogram.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,17 @@ def test_numpy_conversion_5():
939939
assert a1[2, 1] == 5
940940

941941

942+
@pytest.mark.parametrize(
943+
"dtype",
944+
[np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64],
945+
)
946+
def test_fill_dtypes(dtype):
947+
a = bh.Histogram(bh.axis.Integer(0, 2), storage=bh.storage.Int64())
948+
a.fill(np.array([0, 0, 0, 1, 1, 2], dtype=dtype))
949+
a.fill(dtype(0))
950+
assert list(a.values()) == [4, 2]
951+
952+
942953
def test_fill_with_sequence_0():
943954
def fa(*args):
944955
return np.array(args, dtype=float)

0 commit comments

Comments
 (0)