Skip to content

Commit ae4af75

Browse files
jeertmansrossbar
andauthored
ENH: Check that the lengths of the inputs to histogram2d are the same. (numpy#20228)
Improves exception message when inputs have different shapes. Closes numpygh-20050 Co-authored-by: Ross Barnowski <[email protected]>
1 parent 265dd67 commit ae4af75

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

numpy/lib/tests/test_twodim_base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from numpy.core.tests.test_overrides import requires_array_function
1919

2020

21+
import pytest
22+
23+
2124
def get_mat(n):
2225
data = arange(n)
2326
data = add.outer(data, data)
@@ -295,6 +298,13 @@ def __array_function__(self, function, types, args, kwargs):
295298
r = histogram2d(xy, xy, weights=s_d)
296299
assert_(r, ((ShouldDispatch,), (xy, xy), dict(weights=s_d)))
297300

301+
@pytest.mark.parametrize(("x_len", "y_len"), [(10, 11), (20, 19)])
302+
def test_bad_length(self, x_len, y_len):
303+
x, y = np.ones(x_len), np.ones(y_len)
304+
with pytest.raises(ValueError,
305+
match='x and y must have the same length.'):
306+
histogram2d(x, y)
307+
298308

299309
class TestTri:
300310
def test_dtype(self):

numpy/lib/twodim_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,9 @@ def histogram2d(x, y, bins=10, range=None, normed=None, weights=None,
804804
>>> plt.show()
805805
"""
806806
from numpy import histogramdd
807+
808+
if len(x) != len(y):
809+
raise ValueError('x and y must have the same length.')
807810

808811
try:
809812
N = len(bins)

0 commit comments

Comments
 (0)