summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorJérome Eertmans <jeertmans@icloud.com>2021-11-02 16:14:03 +0100
committerGitHub <noreply@github.com>2021-11-02 08:14:03 -0700
commitae4af75cb766d3f5d6b791658ef51a9f16249e8b (patch)
treeec0b32ab4337d8f6d6c546e4163e52d070370ccd /numpy
parent265dd671ea6bc703a9c0adfc5388e54d1f51bd59 (diff)
downloadnumpy-ae4af75cb766d3f5d6b791658ef51a9f16249e8b.tar.gz
ENH: Check that the lengths of the inputs to histogram2d are the same. (#20228)
Improves exception message when inputs have different shapes. Closes gh-20050 Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
Diffstat (limited to 'numpy')
-rw-r--r--numpy/lib/tests/test_twodim_base.py10
-rw-r--r--numpy/lib/twodim_base.py3
2 files changed, 13 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py
index cce683bfe..c1c5a1615 100644
--- a/numpy/lib/tests/test_twodim_base.py
+++ b/numpy/lib/tests/test_twodim_base.py
@@ -18,6 +18,9 @@ import numpy as np
from numpy.core.tests.test_overrides import requires_array_function
+import pytest
+
+
def get_mat(n):
data = arange(n)
data = add.outer(data, data)
@@ -295,6 +298,13 @@ class TestHistogram2d:
r = histogram2d(xy, xy, weights=s_d)
assert_(r, ((ShouldDispatch,), (xy, xy), dict(weights=s_d)))
+ @pytest.mark.parametrize(("x_len", "y_len"), [(10, 11), (20, 19)])
+ def test_bad_length(self, x_len, y_len):
+ x, y = np.ones(x_len), np.ones(y_len)
+ with pytest.raises(ValueError,
+ match='x and y must have the same length.'):
+ histogram2d(x, y)
+
class TestTri:
def test_dtype(self):
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index 811faff79..3e5ad31ff 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -804,6 +804,9 @@ def histogram2d(x, y, bins=10, range=None, normed=None, weights=None,
>>> plt.show()
"""
from numpy import histogramdd
+
+ if len(x) != len(y):
+ raise ValueError('x and y must have the same length.')
try:
N = len(bins)